Skip to content

Commit 0687ab7

Browse files
steipetePeter Steinberger
andauthored
fix(signal): preserve account-owned transport and inbound facts (#116914)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent e922b53 commit 0687ab7

10 files changed

Lines changed: 210 additions & 28 deletions

‎extensions/signal/src/channel.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,12 @@ export const signalPlugin: ChannelPlugin<ResolvedSignalAccount, SignalProbe> =
579579
lastProbeAt: snapshot.lastProbeAt ?? null,
580580
}),
581581
probeAccount: async ({ account, timeoutMs }) => {
582-
const baseUrl = account.baseUrl;
583-
const { probeSignal } = await loadSignalProbeModule();
584-
return await probeSignal(baseUrl, timeoutMs, {
582+
const { probeSignalAccount } = await loadSignalProbeModule();
583+
return await probeSignalAccount({
584+
baseUrl: account.baseUrl,
585+
timeoutMs,
585586
transportKind: account.transport.kind,
587+
account: account.config.account,
586588
});
587589
},
588590
formatCapabilitiesProbe: ({ probe }) =>

‎extensions/signal/src/client-adapter.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,28 @@ describe("signalCheck", () => {
8383
await expect(
8484
signalCheck("http://container:8080", 5_000, { transportKind: "container" }),
8585
).resolves.toEqual({ ok: true, status: 200 });
86-
expect(containerCheck).toHaveBeenCalledWith("http://container:8080", 5_000);
86+
expect(containerCheck).toHaveBeenCalledWith("http://container:8080", 5_000, undefined);
87+
expect(nativeCheck).not.toHaveBeenCalled();
88+
});
89+
90+
it("validates the configured container account's receive WebSocket", async () => {
91+
containerCheck.mockResolvedValue({
92+
ok: false,
93+
status: 200,
94+
error: "Signal container receive endpoint did not upgrade to WebSocket (HTTP 200)",
95+
});
96+
97+
await expect(
98+
signalCheck("http://container:8080", 5_000, {
99+
transportKind: "container",
100+
account: "+15550001111",
101+
}),
102+
).resolves.toEqual({
103+
ok: false,
104+
status: 200,
105+
error: "Signal container receive endpoint did not upgrade to WebSocket (HTTP 200)",
106+
});
107+
expect(containerCheck).toHaveBeenCalledWith("http://container:8080", 5_000, "+15550001111");
87108
expect(nativeCheck).not.toHaveBeenCalled();
88109
});
89110
});

‎extensions/signal/src/client-adapter.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ export async function signalRpcRequest<T = unknown>(
5656
export async function signalCheck(
5757
baseUrl: string,
5858
timeoutMs = DEFAULT_TIMEOUT_MS,
59-
options: { transportKind?: SignalTransportKind } = {},
59+
options: { transportKind?: SignalTransportKind; account?: string } = {},
6060
): Promise<{ ok: boolean; status?: number | null; error?: string | null }> {
6161
try {
6262
return usesContainer(options.transportKind)
63-
? await containerCheck(baseUrl, timeoutMs)
63+
? await containerCheck(baseUrl, timeoutMs, options.account)
6464
: await nativeCheck(baseUrl, timeoutMs);
6565
} catch (error) {
6666
return { ok: false, status: null, error: formatErrorMessage(error) };

‎extensions/signal/src/core.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ describe("probeSignal", () => {
180180
enabled: true,
181181
configured: true,
182182
baseUrl: "http://127.0.0.1:8080",
183+
config: {},
183184
transport: {
184185
kind: "managed-native",
185186
baseUrl: "http://127.0.0.1:8080",
@@ -217,6 +218,59 @@ describe("probeSignal", () => {
217218
expect(res.status).toBe(200);
218219
});
219220

221+
it("preserves every version reported by a Signal REST container", async () => {
222+
vi.spyOn(clientModule, "signalCheck").mockResolvedValueOnce({
223+
ok: true,
224+
status: 200,
225+
error: null,
226+
});
227+
vi.spyOn(clientModule, "signalRpcRequest").mockResolvedValueOnce({
228+
versions: ["v1", "v2"],
229+
build: 42,
230+
});
231+
232+
const result = await probeSignal("http://127.0.0.1:8080", 1000, {
233+
transportKind: "container",
234+
});
235+
236+
expect(result.ok).toBe(true);
237+
expect(result.version).toBe("v1, v2");
238+
});
239+
240+
it("reports container accounts unhealthy when their receive WebSocket cannot upgrade", async () => {
241+
const signalCheck = vi.spyOn(clientModule, "signalCheck").mockResolvedValueOnce({
242+
ok: false,
243+
status: 200,
244+
error: "Signal container receive endpoint did not upgrade to WebSocket (HTTP 200)",
245+
});
246+
const signalRpcRequest = vi.spyOn(clientModule, "signalRpcRequest");
247+
248+
const result = await signalPlugin.status!.probeAccount!({
249+
cfg: {} as never,
250+
account: {
251+
accountId: "default",
252+
enabled: true,
253+
configured: true,
254+
baseUrl: "http://127.0.0.1:8080",
255+
config: { account: "+15550001111" },
256+
transport: { kind: "container", url: "http://127.0.0.1:8080" },
257+
} as never,
258+
timeoutMs: 1000,
259+
});
260+
261+
expect(signalCheck).toHaveBeenCalledWith("http://127.0.0.1:8080", 1000, {
262+
transportKind: "container",
263+
account: "+15550001111",
264+
});
265+
expect(signalRpcRequest).not.toHaveBeenCalled();
266+
expect(result).toMatchObject({
267+
ok: false,
268+
status: 200,
269+
error: "Signal container receive endpoint did not upgrade to WebSocket (HTTP 200)",
270+
version: null,
271+
});
272+
});
273+
220274
it("returns ok=false when /check fails", async () => {
221275
vi.spyOn(clientModule, "signalCheck").mockResolvedValueOnce({
222276
ok: false,

‎extensions/signal/src/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,43 @@ describe("monitorSignalProvider tool results", () => {
924924
expect(hasQueuedReactionEventFor("+15550001111")).toBe(true);
925925
});
926926

927+
it("notifies on own UUID-only reactions using the configured account UUID", async () => {
928+
const accountUuid = "123e4567-e89b-12d3-a456-426614174000";
929+
setReactionNotificationConfig("own", {
930+
account: "+15550002222",
931+
accountUuid,
932+
});
933+
934+
await receiveSingleEnvelope({
935+
...makeBaseEnvelope(),
936+
reactionMessage: {
937+
emoji: "✅",
938+
targetAuthorUuid: accountUuid,
939+
targetSentTimestamp: 2,
940+
},
941+
});
942+
943+
expect(hasQueuedReactionEventFor("+15550001111")).toBe(true);
944+
});
945+
946+
it("does not classify a different account UUID as an own reaction", async () => {
947+
setReactionNotificationConfig("own", {
948+
account: "+15550002222",
949+
accountUuid: "123e4567-e89b-12d3-a456-426614174000",
950+
});
951+
952+
await receiveSingleEnvelope({
953+
...makeBaseEnvelope(),
954+
reactionMessage: {
955+
emoji: "✅",
956+
targetAuthorUuid: "00000000-0000-4000-8000-000000000001",
957+
targetSentTimestamp: 2,
958+
},
959+
});
960+
961+
expect(hasQueuedReactionEventFor("+15550001111")).toBe(false);
962+
});
963+
927964
it("processes messages when reaction metadata is present", async () => {
928965
replyMock.mockResolvedValue({ text: "pong" });
929966

‎extensions/signal/src/monitor.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,31 @@ function isSignalReactionMessage(
183183
function shouldEmitSignalReactionNotification(params: {
184184
mode?: SignalReactionNotificationMode;
185185
account?: string | null;
186+
accountUuid?: string | null;
186187
targets?: SignalReactionTarget[];
187188
sender?: ReturnType<typeof resolveSignalSender> | null;
188189
allowlist?: string[];
189190
}) {
190-
const { mode, account, targets, sender, allowlist } = params;
191+
const { mode, account, accountUuid, targets, sender, allowlist } = params;
191192
const effectiveMode = mode ?? "own";
192193
if (effectiveMode === "off") {
193194
return false;
194195
}
195196
if (effectiveMode === "own") {
196-
const accountId = account?.trim();
197-
if (!accountId || !targets || targets.length === 0) {
197+
const accountId = normalizeOptionalString(account);
198+
const normalizedAccountUuid = normalizeOptionalString(accountUuid);
199+
if ((!accountId && !normalizedAccountUuid) || !targets || targets.length === 0) {
198200
return false;
199201
}
200-
const normalizedAccount = normalizeE164(accountId);
202+
const normalizedAccount = accountId ? normalizeE164(accountId) : undefined;
201203
return targets.some((target) => {
202204
if (target.kind === "uuid") {
203-
return accountId === target.id || accountId === `uuid:${target.id}`;
205+
// UUID-only reaction payloads omit the phone identity carried by account.
206+
return [accountId, normalizedAccountUuid].some(
207+
(candidate) => candidate === target.id || candidate === `uuid:${target.id}`,
208+
);
204209
}
205-
return normalizedAccount === target.id;
210+
return Boolean(normalizedAccount) && normalizedAccount === target.id;
206211
});
207212
}
208213
if (effectiveMode === "allowlist") {

‎extensions/signal/src/monitor/event-handler.mention-gating.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ function getCapturedCtx() {
2424
function getGroupHistoryEntries(
2525
groupHistories: Map<
2626
string,
27-
Array<{ sender?: string; body?: string; media?: HistoryMediaEntry[] }>
27+
Array<{
28+
sender?: string;
29+
body?: string;
30+
media?: HistoryMediaEntry[];
31+
timestamp?: number;
32+
messageId?: string;
33+
}>
2834
>,
2935
groupId = "g1",
3036
) {
@@ -251,6 +257,27 @@ describe("signal mention gating", () => {
251257
expect(entry.body).toBe("hello from alice");
252258
});
253259

260+
it("keeps the canonical data-message timestamp in skipped group history", async () => {
261+
const { handler, groupHistories } = createMentionGatedHistoryHandler();
262+
const timestamp = 1700000000123;
263+
264+
await handler(
265+
createSignalReceiveEvent({
266+
timestamp: undefined,
267+
dataMessage: {
268+
timestamp,
269+
message: "history without a mention",
270+
attachments: [],
271+
groupInfo: { groupId: "g1", groupName: "Test Group" },
272+
},
273+
}),
274+
);
275+
276+
const entry = expectDefined(getGroupHistoryEntries(groupHistories)[0], "Signal history entry");
277+
expect(entry.timestamp).toBe(timestamp);
278+
expect(entry.messageId).toBe(String(timestamp));
279+
});
280+
254281
it("records edited target reply authors for skipped group messages", async () => {
255282
const { handler } = createMentionGatedHistoryHandler();
256283

‎extensions/signal/src/monitor/event-handler.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
811811
const shouldNotify = deps.shouldEmitSignalReactionNotification({
812812
mode: deps.reactionMode,
813813
account: deps.account,
814+
accountUuid: deps.accountUuid,
814815
targets,
815816
sender: params.sender,
816817
allowlist: deps.reactionAllowlist,
@@ -1101,9 +1102,8 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
11011102
sender: envelope.sourceName ?? senderDisplay,
11021103
body: messageText || visibleQuoteText,
11031104
media: toHistoryMediaEntries(pendingMedia),
1104-
timestamp: envelope.timestamp ?? undefined,
1105-
messageId:
1106-
typeof envelope.timestamp === "number" ? String(envelope.timestamp) : undefined,
1105+
timestamp: inboundTimestamp,
1106+
messageId,
11071107
},
11081108
});
11091109
await registerSignalReplyContext({

‎extensions/signal/src/monitor/event-handler.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export type SignalEventHandlerDeps = {
143143
shouldEmitSignalReactionNotification: (params: {
144144
mode?: SignalReactionNotificationMode;
145145
account?: string | null;
146+
accountUuid?: string | null;
146147
targets?: SignalReactionTarget[];
147148
sender?: SignalSender | null;
148149
allowlist?: string[];

‎extensions/signal/src/probe.ts‎

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@ function parseSignalVersion(value: unknown): string | null {
1616
return value.trim();
1717
}
1818
if (typeof value === "object" && value !== null) {
19-
const version = (value as { version?: unknown }).version;
19+
const { version, versions } = value as { version?: unknown; versions?: unknown };
2020
if (typeof version === "string" && version.trim()) {
2121
return version.trim();
2222
}
23+
if (Array.isArray(versions)) {
24+
const normalizedVersions = versions
25+
.filter((entry): entry is string => typeof entry === "string")
26+
.map((entry) => entry.trim())
27+
.filter(Boolean);
28+
if (normalizedVersions.length > 0) {
29+
return normalizedVersions.join(", ");
30+
}
31+
}
2332
}
2433
return null;
2534
}
2635

27-
export async function probeSignal(
36+
type SignalProbeOptions = {
37+
transportKind?: SignalTransportKind;
38+
/** @deprecated Pass transportKind after resolving the account transport. */
39+
apiMode?: "auto" | "native" | "container";
40+
};
41+
42+
async function probeSignalTransport(
2843
baseUrl: string,
2944
timeoutMs: number,
30-
options: {
31-
transportKind?: SignalTransportKind;
32-
/** @deprecated Pass transportKind after resolving the account transport. */
33-
apiMode?: "auto" | "native" | "container";
34-
} = {},
45+
options: SignalProbeOptions,
46+
account?: string,
3547
): Promise<SignalProbe> {
3648
return await runChannelProbe(undefined, async () => {
3749
const result: Omit<SignalProbe, "elapsedMs"> = {
@@ -46,7 +58,10 @@ export async function probeSignal(
4658
} catch (error) {
4759
return { ...result, error: formatErrorMessage(error) };
4860
}
49-
const check = await signalCheck(baseUrl, timeoutMs, { transportKind });
61+
const check = await signalCheck(baseUrl, timeoutMs, {
62+
transportKind,
63+
...(account && transportKind === "container" ? { account } : {}),
64+
});
5065
if (!check.ok) {
5166
return {
5267
...result,
@@ -68,13 +83,33 @@ export async function probeSignal(
6883
});
6984
}
7085

86+
export async function probeSignal(
87+
baseUrl: string,
88+
timeoutMs: number,
89+
options: SignalProbeOptions = {},
90+
): Promise<SignalProbe> {
91+
return await probeSignalTransport(baseUrl, timeoutMs, options);
92+
}
93+
94+
export async function probeSignalAccount(params: {
95+
baseUrl: string;
96+
timeoutMs: number;
97+
transportKind: SignalTransportKind;
98+
account?: string;
99+
}): Promise<SignalProbe> {
100+
// Container /about can succeed while this account's receive WebSocket cannot upgrade.
101+
return await probeSignalTransport(
102+
params.baseUrl,
103+
params.timeoutMs,
104+
{ transportKind: params.transportKind },
105+
params.account,
106+
);
107+
}
108+
71109
async function resolveProbeTransportKind(
72110
baseUrl: string,
73111
timeoutMs: number,
74-
options: {
75-
transportKind?: SignalTransportKind;
76-
apiMode?: "auto" | "native" | "container";
77-
},
112+
options: SignalProbeOptions,
78113
): Promise<SignalTransportKind> {
79114
if (options.transportKind) {
80115
return options.transportKind;

0 commit comments

Comments
 (0)