Skip to content

Commit 8e0666c

Browse files
Michael FethErnie
authored andcommitted
fix(desktop): canonicalize relay pins before ranking Welcome starters
Review follow-up on PR #6037. relayPinRank compared pins normalized only by trimming whitespace and trailing slashes, so backend-equivalent spellings ranked as different communities: ws://localhost:3000 vs ws://127.0.0.1:3000 wss://RELAY.EXAMPLE:443/ vs wss://relay.example ws://relay.example:80 vs ws://relay.example The backend's pair key already folds host case, loopback aliases and default ports, so a record pinned to one spelling of the active community was ranked "pinned elsewhere". On an install that already carries duplicate Welcome records -- the exact population this PR exists to stop growing -- that can reuse the wrong identity. normalizeRelayUrl now delegates to the existing canonicalRelayUrl (features/agents/managedAgentRuntimeStatus.ts) rather than hand-rolling a second normalization. canonicalRelayUrl returns null for anything that is not a ws/wss URL, so a legacy or malformed pin falls back to a stable lowercased form: two records carrying the same bad pin still rank as one community instead of each becoming its own. An unbound pin ("") stays null and keeps its eligible-everywhere rank. Tests cover loopback aliases, host case, both default ports and a trailing slash, plus a malformed pin, and a guard that canonicalization does not fold two genuinely distinct hosts together. A note on how those tests are written: pickAgentForRelay walks every rank and returns the last candidate standing rather than reporting a starter missing, so a single-agent assertion passes with or without canonicalization. My first draft did exactly that and was vacuous -- it passed against the unfixed source. Each case now lists a decoy pinned elsewhere FIRST, so the assertion only holds if the equivalent spelling is actually recognised as rank 0. Verified: reverting welcomeGuide.ts alone takes the file to 25 pass / 2 fail. Full desktop unit suite 4961/4962; the one failure is the pre-existing timing flake in shared/lib/useDocumentVisible.test.mjs, which this branch does not touch. tsc --noEmit and biome clean. Co-authored-by: Michael Feth <[email protected]> Signed-off-by: Michael Feth <[email protected]>
1 parent f05ecea commit 8e0666c

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎desktop/src/features/onboarding/welcomeGuide.test.mjs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,87 @@ test("pickWelcomeGuideAgentForRelay prefers Fizz pinned to the target community"
127127
);
128128
});
129129

130+
test("a relay pin matches its backend-equivalent spellings", () => {
131+
// The backend's pair key folds host case, loopback aliases and default
132+
// ports. Trimming alone treated each spelling as a separate community.
133+
//
134+
// A lone candidate is returned whatever its rank — `pickAgentForRelay` falls
135+
// through every rank rather than reporting a starter missing — so a
136+
// single-agent assertion here would pass with or without canonicalization.
137+
// Each case therefore offers a DECOY listed first: unless the equivalent
138+
// spelling is recognised as rank 0, the decoy wins on array order.
139+
const equivalent = [
140+
["ws://localhost:3000", "ws://127.0.0.1:3000"],
141+
["wss://RELAY.EXAMPLE:443/", "wss://relay.example"],
142+
["ws://relay.example:80", "ws://relay.example"],
143+
["wss://relay.example/", "wss://relay.example"],
144+
];
145+
146+
for (const [pinned, target] of equivalent) {
147+
const decoy = makeAgent({
148+
pubkey: PUB_B,
149+
personaId: WELCOME_GUIDE_PERSONA_ID,
150+
relayUrl: "wss://unrelated.example",
151+
});
152+
const match = makeAgent({
153+
pubkey: PUB_A,
154+
personaId: WELCOME_GUIDE_PERSONA_ID,
155+
relayUrl: pinned,
156+
});
157+
158+
assert.equal(
159+
pickWelcomeGuideAgentForRelay([decoy, match], target),
160+
match,
161+
`${pinned} should rank as pinned to ${target}`,
162+
);
163+
}
164+
});
165+
166+
test("a genuinely different relay is still a different community", () => {
167+
// Canonicalization must fold equivalent spellings without collapsing
168+
// distinct hosts — the failure mode in the other direction.
169+
const pinnedElsewhere = makeAgent({
170+
pubkey: PUB_A,
171+
personaId: WELCOME_GUIDE_PERSONA_ID,
172+
relayUrl: "wss://relay.example",
173+
});
174+
const pinnedHere = makeAgent({
175+
pubkey: PUB_B,
176+
personaId: WELCOME_GUIDE_PERSONA_ID,
177+
relayUrl: "wss://other.example",
178+
});
179+
180+
assert.equal(
181+
pickWelcomeGuideAgentForRelay(
182+
[pinnedElsewhere, pinnedHere],
183+
"wss://other.example",
184+
),
185+
pinnedHere,
186+
);
187+
});
188+
189+
test("a malformed legacy pin still matches itself", () => {
190+
// canonicalRelayUrl returns null for anything that is not a ws/wss URL. Two
191+
// records carrying the same unparseable pin must still rank as the same
192+
// community rather than each becoming its own. Decoy first again, so the
193+
// assertion fails if the malformed pin is not recognised as a match.
194+
const decoy = makeAgent({
195+
pubkey: PUB_B,
196+
personaId: WELCOME_GUIDE_PERSONA_ID,
197+
relayUrl: "wss://unrelated.example",
198+
});
199+
const legacy = makeAgent({
200+
pubkey: PUB_A,
201+
personaId: WELCOME_GUIDE_PERSONA_ID,
202+
relayUrl: "not a url",
203+
});
204+
205+
assert.equal(
206+
pickWelcomeGuideAgentForRelay([decoy, legacy], "NOT A URL "),
207+
legacy,
208+
);
209+
});
210+
130211
test("pickWelcomeGuideAgentForRelay reuses Fizz from another community", () => {
131212
const otherCommunityFizz = makeAgent({
132213
pubkey: PUB_A,

‎desktop/src/features/onboarding/welcomeGuide.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
buildInstanceInputForDefinition,
33
resolveStartRuntimeForDefinition,
44
} from "@/features/agents/lib/instanceInputForDefinition";
5+
import { canonicalRelayUrl } from "@/features/agents/managedAgentRuntimeStatus";
56
import {
67
addChannelMembers,
78
createManagedAgent,
@@ -51,8 +52,27 @@ export type WelcomeTeamAgents = [ManagedAgent, ManagedAgent, ManagedAgent];
5152

5253
const welcomeTeamPromises = new Map<string, Promise<WelcomeTeamAgents>>();
5354

55+
/**
56+
* Comparison key for a relay pin.
57+
*
58+
* Reuses the backend-compatible `canonicalRelayUrl` rather than trimming,
59+
* because the pair key the backend matches on already folds host case,
60+
* loopback aliases (`localhost` / `[::1]` / `127.*`) and default ports.
61+
* Trimming alone ranked `ws://localhost:3000` and `ws://127.0.0.1:3000` as two
62+
* different communities, so on an install that already carries duplicate
63+
* Welcome records the picker could reuse the wrong identity while the backend
64+
* considered both pins the same relay.
65+
*
66+
* A legacy or malformed pin canonicalizes to `null`; it falls back to a stable
67+
* lowercased form so two records carrying the same bad pin still match each
68+
* other instead of each becoming its own community. An unbound pin (`""`, what
69+
* the backend stores when none was supplied) stays `null` and is ranked as
70+
* eligible-everywhere by `relayPinRank`.
71+
*/
5472
function normalizeRelayUrl(relayUrl: string | null | undefined) {
55-
return relayUrl?.trim().replace(/\/+$/, "") ?? null;
73+
const raw = relayUrl?.trim() ?? "";
74+
if (!raw) return null;
75+
return canonicalRelayUrl(raw) ?? raw.toLowerCase().replace(/\/+$/, "");
5676
}
5777

5878
/**

0 commit comments

Comments
 (0)