fix(desktop): harden relay mesh connect p-tag (#834)

This commit is contained in:
tlongwell-block
2026-06-04 11:50:49 -04:00
committed by GitHub
parent b3aefae152
commit 34ac3ba1d1
18 changed files with 750 additions and 148 deletions

View File

@@ -0,0 +1,70 @@
import assert from "node:assert/strict";
import test from "node:test";
import { startManagedAgentWithRules } from "./managedAgentControlActions.ts";
function agent(overrides = {}) {
return {
pubkey: "deadbeef".repeat(8),
name: "Mesh Agent",
personaId: null,
relayUrl: "ws://localhost:3000",
acpCommand: "sprout-acp",
agentCommand: "goose",
agentArgs: [],
mcpCommand: "sprout-mcp-server",
turnTimeoutSeconds: 320,
idleTimeoutSeconds: null,
maxTurnDurationSeconds: null,
parallelism: 1,
systemPrompt: null,
model: "hf://demo/model.gguf",
mcpToolsets: null,
envVars: {},
status: "stopped",
pid: null,
createdAt: new Date(0).toISOString(),
updatedAt: new Date(0).toISOString(),
lastStartedAt: null,
lastStoppedAt: null,
lastExitCode: null,
lastError: null,
logPath: null,
startOnAppLaunch: false,
backend: { type: "local" },
backendAgentId: null,
respondTo: "owner-only",
respondToAllowlist: [],
...overrides,
};
}
test("relay-mesh agents cannot be manually started without a fresh target", async () => {
let called = false;
await assert.rejects(
startManagedAgentWithRules({
agent: agent({
envVars: {
SPROUT_AGENT_PROVIDER: "openai",
OPENAI_COMPAT_BASE_URL: "http://127.0.0.1:9337/v1/",
},
}),
startManagedAgent: async () => {
called = true;
},
}),
/Relay-mesh agents need a fresh serve target/,
);
assert.equal(called, false);
});
test("ordinary local agents still start normally", async () => {
let calledWith = null;
await startManagedAgentWithRules({
agent: agent(),
startManagedAgent: async (pubkey) => {
calledWith = pubkey;
},
});
assert.equal(calledWith, "deadbeef".repeat(8));
});

View File

@@ -75,6 +75,18 @@ export function resolveManagedAgentChannelId(
return matches.length === 1 ? matches[0].id : null;
}
function relayMeshAgentError(agent: ManagedAgent): string | null {
if (agent.backend.type !== "local") return null;
if (agent.envVars.SPROUT_AGENT_PROVIDER !== "openai") return null;
if (
agent.envVars.OPENAI_COMPAT_BASE_URL?.replace(/\/+$/, "") !==
"http://127.0.0.1:9337/v1"
) {
return null;
}
return "Relay-mesh agents need a fresh serve target before start. Create a new agent with Run on relay mesh selected.";
}
export async function startManagedAgentWithRules({
agent,
startManagedAgent,
@@ -82,6 +94,8 @@ export async function startManagedAgentWithRules({
agent: ManagedAgent;
startManagedAgent: StartManagedAgent;
}) {
const relayMeshError = relayMeshAgentError(agent);
if (relayMeshError) throw new Error(relayMeshError);
await startManagedAgent(agent.pubkey);
}
@@ -94,6 +108,8 @@ export async function respawnManagedAgentWithRules({
startManagedAgent: StartManagedAgent;
stopManagedAgent: StopManagedAgent;
}) {
const relayMeshError = relayMeshAgentError(agent);
if (relayMeshError) throw new Error(relayMeshError);
if (agent.backend.type === "local" && isManagedAgentActive(agent)) {
await stopManagedAgent(agent.pubkey);
}

View File

@@ -396,7 +396,9 @@ export function CreateAgentDialog({
envVars,
model: useMesh ? meshModelId.trim() || undefined : undefined,
spawnAfterCreate,
startOnAppLaunch,
// Relay-mesh agents need a freshly selected serve target to start;
// do not auto-restore them later with only the saved model/env.
startOnAppLaunch: useMesh ? false : startOnAppLaunch,
backend: { type: "local" },
...respondToFields,
};

View File

@@ -352,7 +352,9 @@ function AgentActionsMenu({
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<button
aria-label={`Agent actions for ${agent.name}`}
className="flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
data-testid={`managed-agent-actions-${agent.pubkey}`}
type="button"
>
<Ellipsis className="h-4 w-4" />

View File

@@ -1,15 +1,27 @@
import { publishMeshConnectRequest } from "@/shared/api/relayMeshSignaling";
import { getIdentity } from "@/shared/api/tauri";
import {
meshEnsureClientNode,
type MeshServeTarget,
} from "@/shared/api/tauriMesh";
function normalizePubkey(value: string | null | undefined): string | null {
const normalized = value?.trim().toLowerCase() ?? "";
return /^[0-9a-f]{64}$/.test(normalized) ? normalized : null;
}
export async function startRelayMeshClientForTarget(
modelId: string,
target: MeshServeTarget | null,
): Promise<void> {
const status = await meshEnsureClientNode(modelId, target);
if (!target?.reporterPubkey) {
if (!target) {
throw new Error(
"Selected relay mesh target is missing its reporter pubkey.",
);
}
const targetPubkey = normalizePubkey(target.reporterPubkey);
if (!targetPubkey) {
throw new Error(
"Selected relay mesh target is missing its reporter pubkey.",
);
@@ -17,8 +29,18 @@ export async function startRelayMeshClientForTarget(
if (!status.inviteToken) {
throw new Error("Local mesh client did not publish an endpoint address.");
}
const selfPubkey = normalizePubkey((await getIdentity()).pubkey);
if (selfPubkey === targetPubkey) {
// The selected serve target belongs to this desktop. `meshEnsureClientNode`
// has already ensured the local mesh ingress is usable; a relay
// connect-request to ourselves would be rejected as self-targeting and is
// unnecessary for local routing.
return;
}
await publishMeshConnectRequest({
targetPubkey: target.reporterPubkey,
targetPubkey,
selfEndpointAddr: status.inviteToken,
peerEndpointAddr: target.endpointAddr,
attemptId: crypto.randomUUID(),