Skip to content

Commit 6ce88ca

Browse files
wendy-chsyclaude
andauthored
fix(irc): prevent ghost nick collisions on rejoin after network delay (#96108)
* fix(irc): prevent ghost nick collisions on rejoin after network delay * test(irc): add regression tests for fallback nick uniqueness Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: wendy-chsy <[email protected]> Co-authored-by: Claude Opus 4.6 <[email protected]>
1 parent dd4e2ab commit 6ce88ca

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎extensions/irc/src/client.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Irc tests cover client plugin behavior.
22
import { describe, expect, it } from "vitest";
3-
import { buildIrcNickServCommands } from "./client.js";
3+
import { buildFallbackNick, buildIrcNickServCommands } from "./client.js";
44

55
describe("irc client nickserv", () => {
66
it("builds IDENTIFY command when password is set", () => {
@@ -42,3 +42,38 @@ describe("irc client nickserv", () => {
4242
).toEqual(["PRIVMSG NickServ :IDENTIFY secret JOIN #bad"]);
4343
});
4444
});
45+
46+
describe("irc client fallback nick", () => {
47+
it("produces unique fallback nicks across sequential calls", () => {
48+
const first = buildFallbackNick("bot");
49+
const second = buildFallbackNick("bot");
50+
const third = buildFallbackNick("bot");
51+
// First call gets suffix _ (seq=1), subsequent calls get _2, _3, ...
52+
expect(first).toBe("bot_");
53+
expect(second).toMatch(/^bot_\d+$/);
54+
expect(third).toMatch(/^bot_\d+$/);
55+
expect(new Set([first, second, third]).size).toBe(3);
56+
});
57+
58+
it("sanitizes whitespace and special characters in nick", () => {
59+
const nick = buildFallbackNick("my bot!");
60+
expect(nick).toMatch(/^mybot_\d*$/);
61+
});
62+
63+
it("falls back to openclaw when nick consists entirely of special characters", () => {
64+
const nick = buildFallbackNick("!!!");
65+
expect(nick).toMatch(/^openclaw_\d*$/);
66+
});
67+
68+
it("falls back to openclaw when nick is empty after sanitization", () => {
69+
const nick = buildFallbackNick("");
70+
expect(nick).toMatch(/^openclaw_\d*$/);
71+
});
72+
73+
it("truncates long nicks to max 30 chars", () => {
74+
const longNick = "a".repeat(50);
75+
const nick = buildFallbackNick(longNick);
76+
expect(nick.length).toBeLessThanOrEqual(30);
77+
expect(nick).toMatch(/^a+_\d*$/);
78+
});
79+
});

‎extensions/irc/src/client.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ function toError(err: unknown): Error {
6666
return new Error(typeof err === "string" ? err : JSON.stringify(err));
6767
}
6868

69-
function buildFallbackNick(nick: string): string {
69+
let nickCollisionFallbackSeq = 0;
70+
71+
export function buildFallbackNick(nick: string): string {
7072
const normalized = nick.replace(/\s+/g, "");
7173
const safe = normalized.replace(/[^A-Za-z0-9_\-[\]\\`^{}|]/g, "");
7274
const base = safe || "openclaw";
73-
const suffix = "_";
75+
const seq = ++nickCollisionFallbackSeq;
76+
const suffix = seq === 1 ? "_" : `_${seq}`;
7477
const maxNickLen = 30;
7578
if (base.length >= maxNickLen) {
7679
return `${base.slice(0, maxNickLen - suffix.length)}${suffix}`;

0 commit comments

Comments
 (0)