fix(desktop): keep mention prewake cheap and local-only

Address review feedback on the composer mention-wake preflight:

- Bail out of currentPlan when the draft contains no literal "@".
  getMentionOffsets requires one, so no plan is possible without it;
  this skips the mention-ref snapshot (per-entry masked line scans)
  that otherwise ran on every keystroke in mention-free drafts.
- Never start provider-backed agents speculatively. A draft mention
  must not deploy remote compute, so the wake path now filters with
  isProviderBackedAgent and only wakes local agents.

Both behaviors are pinned by new unit tests.

Signed-off-by: Matt Toohey <contact@matttoohey.com>
This commit is contained in:
Matt Toohey
2026-08-26 20:39:07 +10:00
parent f92e29ddc5
commit 21c1b338b5
2 changed files with 92 additions and 5 deletions

View File

@@ -86,7 +86,12 @@ test("editor updates arm a mention-first draft without a rerender", async (t) =>
expectedSignerPubkey: "c".repeat(64),
getDraftMentionRefs: () => [fizzRef],
getManagedAgentsByPubkey: async () =>
new Map([[AGENT, { pubkey: AGENT, status: "stopped" }]]),
new Map([
[
AGENT,
{ pubkey: AGENT, status: "stopped", backend: { type: "local" } },
],
]),
isManagedAgentPubkey: () => true,
memberPubkeys: new Set([AGENT]),
startManagedAgent: async () => {
@@ -103,6 +108,73 @@ test("editor updates arm a mention-first draft without a rerender", async (t) =>
assert.equal(starts, 1);
});
test("mention-free drafts skip the mention-ref snapshot entirely", async () => {
const { act, renderHook } = await import("@testing-library/react");
let snapshots = 0;
const contentRef = { current: "" };
const view = renderHook(() =>
useMentionWakePreflight({
channelId: "general",
contentRef,
enabled: true,
expectedRelayUrl: "wss://relay.example",
expectedSignerPubkey: "c".repeat(64),
getDraftMentionRefs: () => {
snapshots += 1;
return [];
},
getManagedAgentsByPubkey: async () => new Map(),
isManagedAgentPubkey: () => true,
memberPubkeys: new Set([AGENT]),
startManagedAgent: async () => ({ pubkey: AGENT, status: "running" }),
}),
);
contentRef.current = "no mentions in this draft";
act(() => view.result.current.prepareMentionWake(contentRef.current));
assert.equal(snapshots, 0);
});
test("provider-backed agents are never woken speculatively", async (t) => {
t.mock.timers.enable({ apis: ["setTimeout"] });
const { act, renderHook } = await import("@testing-library/react");
let starts = 0;
const contentRef = { current: "@Fizz please investigate" };
const view = renderHook(() =>
useMentionWakePreflight({
channelId: "general",
contentRef,
enabled: true,
expectedRelayUrl: "wss://relay.example",
expectedSignerPubkey: "c".repeat(64),
getDraftMentionRefs: () => [fizzRef],
getManagedAgentsByPubkey: async () =>
new Map([
[
AGENT,
{
pubkey: AGENT,
status: "not_deployed",
backend: { type: "provider", id: "blox", config: {} },
},
],
]),
isManagedAgentPubkey: () => true,
memberPubkeys: new Set([AGENT]),
startManagedAgent: async () => {
starts += 1;
return { pubkey: AGENT, status: "deployed" };
},
}),
);
act(() => view.result.current.prepareMentionWake(contentRef.current));
await act(async () => t.mock.timers.tick(MENTION_WAKE_DELAY_MS));
assert.equal(starts, 0);
});
test("unmount prevents a wake after an in-flight lookup resolves", async (t) => {
t.mock.timers.enable({ apis: ["setTimeout"] });
const { act, renderHook } = await import("@testing-library/react");
@@ -134,7 +206,12 @@ test("unmount prevents a wake after an in-flight lookup resolves", async (t) =>
view.unmount();
await act(async () => {
resolveManagedAgents(
new Map([[AGENT, { pubkey: AGENT, status: "stopped" }]]),
new Map([
[
AGENT,
{ pubkey: AGENT, status: "stopped", backend: { type: "local" } },
],
]),
);
await managedAgents;
});

View File

@@ -4,7 +4,10 @@ import { getMentionOffsets } from "@/features/messages/lib/hasMention";
import type { DraftMentionRef } from "@/features/messages/lib/useDrafts";
import type { ManagedAgent } from "@/shared/api/types";
import { normalizePubkey } from "@/shared/lib/pubkey";
import { isManagedAgentRunning } from "./useMentionSendFlow.helpers";
import {
isManagedAgentRunning,
isProviderBackedAgent,
} from "./useMentionSendFlow.helpers";
export const MENTION_WAKE_DELAY_MS = 1_000;
@@ -127,7 +130,10 @@ export function useMentionWakePreflight({
if (
!context.enabled ||
!context.expectedRelayUrl ||
!context.expectedSignerPubkey
!context.expectedSignerPubkey ||
// Mentions require a literal "@" (see getMentionOffsets), so drafts
// without one can't produce a plan — skip the per-keystroke ref scan.
!content.includes("@")
) {
return null;
}
@@ -175,7 +181,11 @@ export function useMentionWakePreflight({
await Promise.allSettled(
beforeWake.pubkeys.flatMap((pubkey) => {
const agent = managedAgents.get(pubkey);
return agent && !isManagedAgentRunning(agent)
// A draft mention must never deploy remote compute; only the
// send path may start provider-backed agents.
return agent &&
!isProviderBackedAgent(agent) &&
!isManagedAgentRunning(agent)
? [
context.startManagedAgent({
pubkey,