mirror of
https://github.com/block/buzz.git
synced 2026-11-27 03:31:50 +00:00
🤖 Leave contacts the relay before it removes anything locally: it checks the relay's `/info` for NIP-43 membership support, then waits for the relay to accept a signed leave request. When a relay has been torn down, that check fails, Leave throws, and the community stays in the rail. Removing a community also left its relay's managed agent pairs running, and they kept reconnecting to it until the app was quit. **Remove from this device** - A new community action, **Remove from this device**, sits under **Leave community** in the profile menu's community actions. It never contacts the relay. A confirm dialog explains that relay membership isn't revoked. - `useCommunityNavigationTransitions` splits the old `removeCommunity` into `removeCommunityFromDevice` (local-only cleanup and navigation to a fallback community or the discovery screen) and `leaveAndRemoveCommunity` (calls `leaveCommunity`, then runs the same cleanup). Leave's flow hasn't changed. - When Leave fails, the error now adds: "If the community no longer exists, use Remove from this device." The error shown when community discovery state can't be saved no longer says "Membership was removed", so its wording fits both actions. **Agent pairs on a removed relay** - Once the local removal completes, the shared cleanup stops every live agent pair on that relay, including manually started ones. It matches relays with `canonicalRelayUrl`. Stopping is best-effort: failures are logged and the removal still completes. Pairs on other relays aren't touched, and nothing is stopped if another community still uses the same relay. - Both reconcile call sites (`useManagedAgentRuntimeReconciliation` and `bootstrapManagedAgentRuntimePairs`) now go through `reconcileConfiguredManagedAgentRuntimes`. A reconcile that is already running in Rust can't be cancelled, so after it resolves, this helper stops any pair it started whose relay is no longer in the saved community list. - Launch restore runs in the background after `apply_workspace` and can finish after its community is removed. `apply_workspace` takes a restore ticket (the current `launch_restore_generation` in `AppState`) before handing its lock to the restore task. Removing the active community calls the new `invalidate_launch_restore` command, which bumps that generation while holding the runtime transition lock. `restore_managed_agents_on_launch` re-checks its ticket under the same lock and spawns nothing if it changed. If restore takes the lock first, its pairs are registered before invalidation returns, and the removal stop then finds them. Removal never changes `relay_url_override`: the outgoing relay stays applied until the next `apply_workspace`, exactly as on `main`, so in-flight work never falls back to the default relay. Invalidation never takes `workspace_apply_lock`, which restore holds while waiting on the transition lock, so lock order is unchanged. - With no relay applied, backend readers fall back to the default relay, the same state as launch before the first `apply_workspace`. A normal quit and relaunch doesn't bring a pair back on a removed relay: restore targets the applied workspace, and the legacy per-record relay pin is ignored. **Tests** - A `community-rail` e2e case makes the mocked relay's membership check fail, the way an unreachable relay does. It checks that Leave shows the error and keeps the community, and that Remove from this device then removes it, switches to the fallback, stops the agent pair on the removed relay, and leaves the pair on the other relay running. - Unit tests in `managedAgentRelayCleanup.test.mjs` cover relay matching, a stop or listing failure not throwing, and the reconcile fence stopping a pair that started on a relay removed mid-flight. --------- Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
120 lines
3.9 KiB
TypeScript
120 lines
3.9 KiB
TypeScript
import type { ManagedAgentRuntimeStatus } from "@/shared/api/types";
|
|
|
|
export type AgentCommunityAvailability =
|
|
| "Here"
|
|
| "Waking"
|
|
| "Needs setup on this device"
|
|
| "Unavailable";
|
|
|
|
export function agentCommunityAvailability(
|
|
runtime: ManagedAgentRuntimeStatus,
|
|
): AgentCommunityAvailability {
|
|
if (!runtime.localSetup) return "Needs setup on this device";
|
|
|
|
switch (runtime.lifecycle) {
|
|
case "starting":
|
|
case "listening":
|
|
case "waking":
|
|
return "Waking";
|
|
case "ready":
|
|
return "Here";
|
|
case "failed":
|
|
case "stopped":
|
|
return "Unavailable";
|
|
}
|
|
}
|
|
|
|
export function agentCommunityStatusDetail(
|
|
runtime: ManagedAgentRuntimeStatus,
|
|
): string | null {
|
|
if (!runtime.localSetup)
|
|
return "Set up this agent on this device to start it.";
|
|
if (runtime.lifecycle === "stopped") return "Stopped by you";
|
|
if (runtime.lifecycle === "failed")
|
|
return runtime.error ?? "Could not connect";
|
|
return null;
|
|
}
|
|
|
|
export function managedAgentRuntimeKey(
|
|
runtime: Pick<ManagedAgentRuntimeStatus, "pubkey" | "relayUrl">,
|
|
): string {
|
|
return JSON.stringify([runtime.pubkey, runtime.relayUrl]);
|
|
}
|
|
|
|
export type ManagedAgentPairAction = "start" | "stop" | "restart";
|
|
|
|
/** Menu action for one agent+community pair. A missing runtime row means the
|
|
* pair is not running here, so the only sensible action is to start it. */
|
|
export function managedAgentPairAction(
|
|
runtime: ManagedAgentRuntimeStatus | undefined,
|
|
): ManagedAgentPairAction {
|
|
if (!runtime || runtime.lifecycle === "stopped") return "start";
|
|
if (runtime.lifecycle === "failed") return "restart";
|
|
return "stop";
|
|
}
|
|
|
|
export const MANAGED_AGENT_PAIR_ACTION_LABELS: Record<
|
|
ManagedAgentPairAction,
|
|
string
|
|
> = {
|
|
start: "Start Agent",
|
|
stop: "Stop Agent",
|
|
restart: "Restart Agent",
|
|
};
|
|
|
|
/**
|
|
* Canonicalize a relay URL the way the backend keys runtime pairs, so a
|
|
* stored community URL (e.g. `ws://localhost:3000`) matches backend rows
|
|
* (`ws://127.0.0.1:3000`). Mirrors buzz-core's `normalize_relay_url`
|
|
* (`crates/buzz-core/src/relay.rs`): lowercase host, `localhost` and loopback IP
|
|
* addresses folded to 127.0.0.1, default ports and root-path trailing slash stripped.
|
|
* Returns null when the URL cannot be parsed as ws/wss.
|
|
*/
|
|
export function canonicalRelayUrl(raw: string): string | null {
|
|
let url: URL;
|
|
try {
|
|
url = new URL(raw.trim());
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (url.protocol !== "ws:" && url.protocol !== "wss:") return null;
|
|
let host = url.hostname.toLowerCase();
|
|
// `URL` normalizes IP literals, so only a real 127.0.0.0/8 address or the
|
|
// IPv6 loopback matches here; a domain such as `127.preview.example` does not.
|
|
if (
|
|
host === "localhost" ||
|
|
host === "[::1]" ||
|
|
/^127\.\d+\.\d+\.\d+$/.test(host)
|
|
) {
|
|
host = "127.0.0.1";
|
|
}
|
|
const defaultPort = url.protocol === "ws:" ? "80" : "443";
|
|
const port = url.port && url.port !== defaultPort ? `:${url.port}` : "";
|
|
const path = url.pathname === "/" ? "" : url.pathname;
|
|
// The backend trims trailing slashes from the final rendered URL.
|
|
return `${url.protocol}//${host}${port}${path}${url.search}`.replace(
|
|
/\/+$/,
|
|
"",
|
|
);
|
|
}
|
|
|
|
export function findManagedAgentRuntime(
|
|
runtimes: readonly ManagedAgentRuntimeStatus[],
|
|
pubkey: string,
|
|
relayUrl: string,
|
|
): ManagedAgentRuntimeStatus | undefined {
|
|
const normalizedPubkey = pubkey.toLowerCase();
|
|
// Backend rows carry the canonical pair URL; the caller passes the
|
|
// community's stored URL, which may differ in spelling (localhost vs
|
|
// 127.0.0.1, default port, trailing slash). Compare canonically, keeping
|
|
// the exact-string checks as a fallback for unparsable stored URLs.
|
|
const canonical = canonicalRelayUrl(relayUrl);
|
|
return runtimes.find(
|
|
(runtime) =>
|
|
runtime.pubkey.toLowerCase() === normalizedPubkey &&
|
|
(runtime.relayUrl === relayUrl ||
|
|
runtime.requestedRelayUrl === relayUrl ||
|
|
(canonical !== null && runtime.relayUrl === canonical)),
|
|
);
|
|
}
|