Skip to content

Commit 17af15e

Browse files
authored
fix(desktop): keep member runtime status off the UI thread (#6445)
## Summary - skip managed-agent runtime discovery when the members sidebar has no local managed bots - run runtime listing disk, process, and mutex work on Tauri’s blocking pool - preserve local managed-bot status and Start/Stop behavior with positive and negative E2E coverage Opening Add people in a human-only channel could invoke synchronous native runtime discovery before the sidebar painted, leaving the macOS app beachballed. Human invites do not depend on that data. ## Why I have seen slowness opening this dialog in the UI https://github.com/user-attachments/assets/1955eb5e-ee47-4edf-8e5c-606d11ffbc25 ### Related issue Related overlap: #4851 is a broader managed-agent lifecycle change that includes a similar native offload. This draft is intentionally limited to the sidebar critical path and adds the human-only query gate. ### Testing I have verified the pause in the video goes away after this change. - `just ci` - `just desktop-check` - `just desktop-test` (5,241 passed) - `just desktop-tauri-fmt-check` - `just desktop-tauri-clippy` - `just desktop-tauri-test` (2,702 passed; 18 ignored) - focused Playwright: human-only sidebar skips runtime discovery - focused Playwright: local managed bot retains status and Stop/Start controls No visual styling changed, so screenshots are not applicable. Signed-off-by: Matt Toohey <[email protected]>
1 parent 2d28037 commit 17af15e

3 files changed

Lines changed: 125 additions & 71 deletions

File tree

‎desktop/src-tauri/src/managed_agents/runtime_commands.rs‎

Lines changed: 85 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -137,86 +137,91 @@ pub fn put_managed_agent_runtime_lifecycle(
137137
Ok(status)
138138
}
139139

140+
// Keep disk, process, and mutex work off the main thread so opening members cannot stall the UI.
140141
#[tauri::command]
141-
pub fn list_managed_agent_runtimes(
142+
pub async fn list_managed_agent_runtimes(
142143
app: AppHandle,
143144
) -> Result<Vec<ManagedAgentRuntimeStatus>, String> {
144-
// This command is polled whenever the members sidebar opens and refetched
145-
// on every status event — load the per-row status inputs once, outside
146-
// the locks, instead of hitting disk per row while holding them.
147-
let personas = load_personas(&app).unwrap_or_default();
148-
let global = load_global_agent_config(&app).unwrap_or_default();
149-
let state = app.state::<AppState>();
150-
let _transition = state
151-
.managed_agent_runtime_transition
152-
.lock()
153-
.map_err(|e| e.to_string())?;
154-
let _store = state
155-
.managed_agents_store_lock
156-
.lock()
157-
.map_err(|e| e.to_string())?;
158-
let mut records = load_managed_agents(&app)?;
159-
let mut runtimes = state
160-
.managed_agent_processes
161-
.lock()
162-
.map_err(|e| e.to_string())?;
163-
let exited_keys: Vec<_> = runtimes
164-
.iter_mut()
165-
.filter_map(|(key, runtime)| match runtime.child.try_wait() {
166-
Ok(Some(_)) | Err(_) => Some(key.clone()),
167-
Ok(None) => None,
168-
})
169-
.collect();
170-
let records_changed = !exited_keys.is_empty();
171-
let mut statuses = Vec::new();
172-
for key in exited_keys {
173-
runtimes.remove(&key);
174-
super::remove_agent_runtime_receipt(&app, &key);
175-
state.clear_agent_session_cache(&key);
176-
if let Some(record) = records
145+
tokio::task::spawn_blocking(move || {
146+
// This command is polled whenever the members sidebar opens and refetched
147+
// on every status event — load the per-row status inputs once, outside
148+
// the locks, instead of hitting disk per row while holding them.
149+
let personas = load_personas(&app).unwrap_or_default();
150+
let global = load_global_agent_config(&app).unwrap_or_default();
151+
let state = app.state::<AppState>();
152+
let _transition = state
153+
.managed_agent_runtime_transition
154+
.lock()
155+
.map_err(|e| e.to_string())?;
156+
let _store = state
157+
.managed_agents_store_lock
158+
.lock()
159+
.map_err(|e| e.to_string())?;
160+
let mut records = load_managed_agents(&app)?;
161+
let mut runtimes = state
162+
.managed_agent_processes
163+
.lock()
164+
.map_err(|e| e.to_string())?;
165+
let exited_keys: Vec<_> = runtimes
177166
.iter_mut()
178-
.find(|record| record.pubkey.eq_ignore_ascii_case(&key.pubkey))
179-
{
180-
record.updated_at = crate::util::now_iso();
181-
record.last_stopped_at = Some(record.updated_at.clone());
182-
let status = status_for_with(
167+
.filter_map(|(key, runtime)| match runtime.child.try_wait() {
168+
Ok(Some(_)) | Err(_) => Some(key.clone()),
169+
Ok(None) => None,
170+
})
171+
.collect();
172+
let records_changed = !exited_keys.is_empty();
173+
let mut statuses = Vec::new();
174+
for key in exited_keys {
175+
runtimes.remove(&key);
176+
super::remove_agent_runtime_receipt(&app, &key);
177+
state.clear_agent_session_cache(&key);
178+
if let Some(record) = records
179+
.iter_mut()
180+
.find(|record| record.pubkey.eq_ignore_ascii_case(&key.pubkey))
181+
{
182+
record.updated_at = crate::util::now_iso();
183+
record.last_stopped_at = Some(record.updated_at.clone());
184+
let status = status_for_with(
185+
&app,
186+
record,
187+
&key,
188+
None,
189+
None,
190+
StatusInputs {
191+
personas: &personas,
192+
global: &global,
193+
},
194+
);
195+
emit_status(&app, &status);
196+
statuses.push(status);
197+
}
198+
}
199+
statuses.extend(runtimes.iter().filter_map(|(key, runtime)| {
200+
let record = records
201+
.iter()
202+
.find(|record| record.pubkey.eq_ignore_ascii_case(&key.pubkey))?;
203+
Some(status_for_with(
183204
&app,
184205
record,
185-
&key,
186-
None,
206+
key,
207+
Some(runtime),
187208
None,
188209
StatusInputs {
189210
personas: &personas,
190211
global: &global,
191212
},
192-
);
193-
emit_status(&app, &status);
194-
statuses.push(status);
213+
))
214+
}));
215+
drop(runtimes);
216+
// Records are only mutated above when a runtime exited — skip the store
217+
// rewrite on the common nothing-changed poll.
218+
if records_changed {
219+
save_managed_agents(&app, &records)?;
195220
}
196-
}
197-
statuses.extend(runtimes.iter().filter_map(|(key, runtime)| {
198-
let record = records
199-
.iter()
200-
.find(|record| record.pubkey.eq_ignore_ascii_case(&key.pubkey))?;
201-
Some(status_for_with(
202-
&app,
203-
record,
204-
key,
205-
Some(runtime),
206-
None,
207-
StatusInputs {
208-
personas: &personas,
209-
global: &global,
210-
},
211-
))
212-
}));
213-
drop(runtimes);
214-
// Records are only mutated above when a runtime exited — skip the store
215-
// rewrite on the common nothing-changed poll.
216-
if records_changed {
217-
save_managed_agents(&app, &records)?;
218-
}
219-
Ok(statuses)
221+
Ok(statuses)
222+
})
223+
.await
224+
.map_err(|e| format!("spawn_blocking failed: {e}"))?
220225
}
221226

222227
pub(crate) fn start_managed_agent_runtime_pair_lazy(
@@ -572,6 +577,18 @@ pub async fn reconcile_managed_agent_runtimes(
572577
mod tests {
573578
use super::*;
574579

580+
#[test]
581+
fn list_managed_agent_runtimes_returns_a_future() {
582+
fn assert_async_command<F, Fut>(_command: F)
583+
where
584+
F: Fn(AppHandle) -> Fut,
585+
Fut: std::future::Future<Output = Result<Vec<ManagedAgentRuntimeStatus>, String>>,
586+
{
587+
}
588+
589+
assert_async_command(list_managed_agent_runtimes);
590+
}
591+
575592
fn payload(
576593
relay_url: &str,
577594
lifecycle: ManagedAgentRuntimeLifecycle,

‎desktop/src/features/channels/ui/MembersSidebar.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ export function MembersSidebar({
141141
relayUrl,
142142
}: MembersSidebarProps) {
143143
const channelId = channel?.id ?? null;
144-
const managedAgentRuntimesQuery = useManagedAgentRuntimesQuery({
145-
enabled: open,
146-
});
147144
const queryClient = useQueryClient();
148145
const searchInputRef = React.useRef<HTMLInputElement>(null);
149146
const [searchQuery, setSearchQuery] = React.useState("");
@@ -470,6 +467,18 @@ export function MembersSidebar({
470467
),
471468
[managedAgentsQuery.data],
472469
);
470+
const hasLocalManagedMember = React.useMemo(
471+
() =>
472+
[...bots, ...archived].some(
473+
(member) =>
474+
managedAgentByPubkey.get(normalizePubkey(member.pubkey))?.backend
475+
.type === "local",
476+
),
477+
[archived, bots, managedAgentByPubkey],
478+
);
479+
const managedAgentRuntimesQuery = useManagedAgentRuntimesQuery({
480+
enabled: open && Boolean(relayUrl) && hasLocalManagedMember,
481+
});
473482
const controllableManagedBots = React.useMemo(
474483
() =>
475484
bots.flatMap((member) => {

‎desktop/tests/e2e/channels.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4017,6 +4017,25 @@ test("members sidebar virtualizes large channel rosters", async ({ page }) => {
40174017
).toBeVisible();
40184018
});
40194019

4020+
test("opening a human-only members sidebar skips managed runtime discovery", async ({
4021+
page,
4022+
}) => {
4023+
await page.goto("/");
4024+
const baselineCommands = await readCommandLog(page);
4025+
const baselineRuntimeListCount = commandCount(
4026+
baselineCommands,
4027+
"list_managed_agent_runtimes",
4028+
);
4029+
4030+
await openMembersSidebar(page, "random");
4031+
await expect(page.getByTestId("members-sidebar-people")).toBeVisible();
4032+
4033+
const commands = await readCommandLog(page);
4034+
expect(commandCount(commands, "list_managed_agent_runtimes")).toBe(
4035+
baselineRuntimeListCount,
4036+
);
4037+
});
4038+
40204039
test("members sidebar can invite relay-authorized agents", async ({ page }) => {
40214040
await installMockBridge(page, {
40224041
relayAgents: [
@@ -4528,8 +4547,17 @@ test("members sidebar can stop and start a managed bot in this community", async
45284547
baselineCommands,
45294548
"stop_managed_agent",
45304549
);
4550+
const baselineRuntimeListCount = commandCount(
4551+
baselineCommands,
4552+
"list_managed_agent_runtimes",
4553+
);
45314554

45324555
await openMembersSidebar(page, "general");
4556+
await expect
4557+
.poll(async () =>
4558+
commandCount(await readCommandLog(page), "list_managed_agent_runtimes"),
4559+
)
4560+
.toBe(baselineRuntimeListCount + 1);
45334561

45344562
const agentStatus = page.getByTestId(
45354563
`sidebar-managed-agent-status-${agentPubkey}`,

0 commit comments

Comments
 (0)