Skip to content

Commit 510009c

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
andauthored
fix(desktop): allow restarting saved relay-mesh agents from the UI (#956)
Signed-off-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
1 parent 60c8c50 commit 510009c

4 files changed

Lines changed: 55 additions & 37 deletions

File tree

‎desktop/src/features/agents/lib/managedAgentControlActions.test.mjs‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,33 @@ function agent(overrides = {}) {
3939
};
4040
}
4141

42-
test("relay-mesh agents cannot be manually started without a fresh target", async () => {
43-
let called = false;
42+
test("relay-mesh agents delegate start to the backend preflight", async () => {
43+
const meshAgent = agent({
44+
envVars: {
45+
SPROUT_AGENT_PROVIDER: "openai",
46+
OPENAI_COMPAT_BASE_URL: "http://127.0.0.1:9337/v1/",
47+
},
48+
});
49+
50+
let calledWith = null;
51+
await startManagedAgentWithRules({
52+
agent: meshAgent,
53+
startManagedAgent: async (pubkey) => {
54+
calledWith = pubkey;
55+
},
56+
});
57+
assert.equal(calledWith, meshAgent.pubkey);
58+
59+
// Backend preflight failures (e.g. no live serve target) propagate as-is.
4460
await assert.rejects(
4561
startManagedAgentWithRules({
46-
agent: agent({
47-
envVars: {
48-
SPROUT_AGENT_PROVIDER: "openai",
49-
OPENAI_COMPAT_BASE_URL: "http://127.0.0.1:9337/v1/",
50-
},
51-
}),
62+
agent: meshAgent,
5263
startManagedAgent: async () => {
53-
called = true;
64+
throw new Error("no live serve target is available for this model");
5465
},
5566
}),
56-
/Relay-mesh agents need a fresh serve target/,
67+
/no live serve target/,
5768
);
58-
assert.equal(called, false);
5969
});
6070

6171
test("ordinary local agents still start normally", async () => {

‎desktop/src/features/agents/lib/managedAgentControlActions.ts‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,16 @@ export function resolveManagedAgentChannelId(
7575
return matches.length === 1 ? matches[0].id : null;
7676
}
7777

78-
function relayMeshAgentError(agent: ManagedAgent): string | null {
79-
if (agent.backend.type !== "local") return null;
80-
if (agent.envVars.SPROUT_AGENT_PROVIDER !== "openai") return null;
81-
if (
82-
agent.envVars.OPENAI_COMPAT_BASE_URL?.replace(/\/+$/, "") !==
83-
"http://127.0.0.1:9337/v1"
84-
) {
85-
return null;
86-
}
87-
return "Relay-mesh agents need a fresh serve target before start. Create a new agent with Run on relay mesh selected.";
88-
}
89-
9078
export async function startManagedAgentWithRules({
9179
agent,
9280
startManagedAgent,
9381
}: {
9482
agent: ManagedAgent;
9583
startManagedAgent: StartManagedAgent;
9684
}) {
97-
const relayMeshError = relayMeshAgentError(agent);
98-
if (relayMeshError) throw new Error(relayMeshError);
85+
// Relay-mesh agents are no longer blocked here: the backend start preflight
86+
// (ensure_relay_mesh_for_record) re-resolves a live serve target and dials
87+
// it, failing with an actionable error when no peer serves the model.
9988
await startManagedAgent(agent.pubkey);
10089
}
10190

@@ -108,8 +97,6 @@ export async function respawnManagedAgentWithRules({
10897
startManagedAgent: StartManagedAgent;
10998
stopManagedAgent: StopManagedAgent;
11099
}) {
111-
const relayMeshError = relayMeshAgentError(agent);
112-
if (relayMeshError) throw new Error(relayMeshError);
113100
if (agent.backend.type === "local" && isManagedAgentActive(agent)) {
114101
await stopManagedAgent(agent.pubkey);
115102
}

‎desktop/src/testing/e2eBridge.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4765,9 +4765,18 @@ async function handleStartManagedAgent(args: {
47654765
}): Promise<RawManagedAgent> {
47664766
const agent = getMockManagedAgent(args.pubkey);
47674767
if (isRelayMeshManagedAgent(agent)) {
4768-
throw new Error(
4769-
"relay mesh agents cannot be started from saved state because the selected serve target is not persisted. Create a new agent with Run on relay mesh selected to refresh the target for http://127.0.0.1:9337/v1.",
4770-
);
4768+
// Model the backend start preflight (ensure_relay_mesh_for_record): a
4769+
// saved relay-mesh agent re-resolves a live serve target for its model
4770+
// and only fails when no peer currently serves it.
4771+
const modelId = agent.env_vars?.OPENAI_COMPAT_MODEL;
4772+
const hasLiveTarget =
4773+
mockMeshState.admitted &&
4774+
mockMeshState.models.some((model) => model.id === modelId);
4775+
if (!hasLiveTarget) {
4776+
throw new Error(
4777+
"relay mesh agents cannot be started from saved state because no live serve target is available for this model. Start serving on a mesh peer, or create a new agent with Run on relay mesh selected to refresh the target for http://127.0.0.1:9337/v1.",
4778+
);
4779+
}
47714780
}
47724781

47734782
const now = new Date().toISOString();

‎desktop/tests/e2e/mesh-compute.spec.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ test("a non-member cannot enable relay-mesh — membership is the gate", async (
278278
expect(seq).not.toContain("create_managed_agent");
279279
});
280280

281-
test("saved relay-mesh agents require a fresh serve target before manual start", async ({
281+
test("saved relay-mesh agents restart via the backend serve-target preflight", async ({
282282
page,
283283
}) => {
284284
await gotoApp(page);
@@ -330,18 +330,30 @@ test("saved relay-mesh agents require a fresh serve target before manual start",
330330
.toContain("stop_managed_agent");
331331
await expect(row).toContainText("stopped");
332332

333-
const before = (await commands(page)).length;
333+
// With a live serve target for the model, manual restart goes through:
334+
// the backend preflight re-resolves the target and the agent starts.
335+
await openManagedAgentActions(page, pubkey);
336+
await page.getByRole("menuitem", { name: "Spawn" }).click();
337+
await expect
338+
.poll(async () => await commands(page))
339+
.toContain("start_managed_agent");
340+
await expect(row).toContainText("running");
341+
342+
await openManagedAgentActions(page, pubkey);
343+
await page.getByRole("menuitem", { name: "Stop" }).click();
344+
await expect(row).toContainText("stopped");
345+
346+
// Without a live serve target, the backend preflight rejects the start
347+
// with an actionable error, surfaced as a toast; the agent stays stopped.
348+
await setMesh(page, { models: [] });
334349
await openManagedAgentActions(page, pubkey);
335350
await page.getByRole("menuitem", { name: "Spawn" }).click();
336351

337352
await expect(
338353
page
339354
.locator("[data-sonner-toast]")
340-
.filter({ hasText: "Relay-mesh agents need a fresh serve target" }),
355+
.filter({ hasText: "no live serve target is available" }),
341356
).toBeVisible();
342-
expect((await commands(page)).slice(before)).not.toContain(
343-
"start_managed_agent",
344-
);
345357
await expect(row).toContainText("stopped");
346358

347359
await expect(
@@ -360,6 +372,6 @@ test("saved relay-mesh agents require a fresh serve target before manual start",
360372
return err instanceof Error ? err.message : String(err);
361373
}
362374
}, pubkey),
363-
).resolves.toContain("selected serve target is not persisted");
375+
).resolves.toContain("no live serve target is available");
364376
await expect(row).toContainText("stopped");
365377
});

0 commit comments

Comments
 (0)