Skip to content

Commit 9690975

Browse files
committed
fix(googlechat): report a blocked account instead of a webhook path that was never bound
An unparseable webhookUrl resolves to no path, and the monitor bails before it creates ingress or registers the HTTP route. The gateway still published the default path, and because this channel never sets connected, health evaluation read healthy for the lifetime of the process while inbound delivery was dead. Make resolveGoogleChatWebhookPath return string | null and have the gateway publish lifecycle: blocked with lastError instead of a path. The status store patch-merges, so the blocked branch clears webhookPath explicitly.
1 parent a530a7b commit 9690975

3 files changed

Lines changed: 128 additions & 19 deletions

File tree

‎extensions/googlechat/src/gateway.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const loadGoogleChatChannelRuntime = createLazyRuntimeNamedExport(
1818
"googleChatChannelRuntime",
1919
);
2020

21+
const UNRESOLVED_WEBHOOK_URL_ERROR =
22+
"Invalid webhookUrl: expected an absolute URL such as https://chat.example.com/googlechat. No inbound webhook route was registered.";
23+
2124
export async function startGoogleChatGatewayAccount(ctx: {
2225
account: ResolvedGoogleChatAccount;
2326
cfg: OpenClawConfig;
@@ -37,10 +40,23 @@ export async function startGoogleChatGatewayAccount(ctx: {
3740
ctx.log?.info?.(`[${account.accountId}] starting Google Chat webhook`);
3841
const { resolveGoogleChatWebhookPath, startGoogleChatMonitor } =
3942
await loadGoogleChatChannelRuntime();
43+
// An unparseable webhookUrl resolves to no path, and the monitor bails before it
44+
// creates ingress or registers the HTTP route. Reporting the default path instead
45+
// would advertise a route nothing binds, and this channel never sets `connected`,
46+
// so health evaluation would read "healthy" for the lifetime of the process.
47+
// The status store patch-merges, so the blocked branch clears `webhookPath`
48+
// explicitly: a restart with broken config must not keep the previous run's path.
49+
const webhookPath = resolveGoogleChatWebhookPath({ account });
4050
statusSink({
4151
running: true,
4252
lastStartAt: Date.now(),
43-
webhookPath: resolveGoogleChatWebhookPath({ account }),
53+
...(webhookPath
54+
? { webhookPath }
55+
: {
56+
webhookPath: undefined,
57+
lifecycle: "blocked" as const,
58+
lastError: UNRESOLVED_WEBHOOK_URL_ERROR,
59+
}),
4460
audienceType: account.config.audienceType,
4561
audience: account.config.audience,
4662
});

‎extensions/googlechat/src/monitor.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,15 @@ export async function startGoogleChatMonitor(
545545
return await monitorGoogleChatProvider(params);
546546
}
547547

548+
// Null keeps the same meaning it has in monitorGoogleChatProvider above: the
549+
// configured webhookUrl does not parse, so no route is ever bound. Falling back
550+
// to the default path here would report a route the monitor never registers.
548551
export function resolveGoogleChatWebhookPath(params: {
549552
account: ResolvedGoogleChatAccount;
550-
}): string {
551-
return (
552-
resolveWebhookPath({
553-
webhookPath: params.account.config.webhookPath,
554-
webhookUrl: params.account.config.webhookUrl,
555-
defaultPath: "/googlechat",
556-
}) ?? "/googlechat"
557-
);
553+
}): string | null {
554+
return resolveWebhookPath({
555+
webhookPath: params.account.config.webhookPath,
556+
webhookUrl: params.account.config.webhookUrl,
557+
defaultPath: "/googlechat",
558+
});
558559
}

‎extensions/googlechat/src/setup.test.ts‎

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ const hoisted = vi.hoisted(() => ({
3434
startGoogleChatMonitor: vi.fn(),
3535
}));
3636

37-
vi.mock("./channel.runtime.js", () => ({
38-
googleChatChannelRuntime: {
39-
resolveGoogleChatWebhookPath: ({
40-
account,
41-
}: {
42-
account: { config: { webhookPath?: string } };
43-
}) => account.config.webhookPath ?? "/googlechat",
44-
startGoogleChatMonitor: hoisted.startGoogleChatMonitor,
45-
},
46-
}));
37+
// The path resolver stays real so the status assertions below cover the whole chain
38+
// from configured webhookUrl to published snapshot; only the monitor is stubbed.
39+
vi.mock("./channel.runtime.js", async () => {
40+
const monitor = await vi.importActual<typeof import("./monitor.js")>("./monitor.js");
41+
return {
42+
googleChatChannelRuntime: {
43+
resolveGoogleChatWebhookPath: monitor.resolveGoogleChatWebhookPath,
44+
startGoogleChatMonitor: hoisted.startGoogleChatMonitor,
45+
},
46+
};
47+
});
4748

4849
const googlechatSetupPlugin = {
4950
id: "googlechat",
@@ -364,6 +365,97 @@ describe("googlechat setup", () => {
364365
expectLifecyclePatch(patches, { running: false });
365366
});
366367

368+
it("publishes the bound webhook path when the account resolves one", async () => {
369+
hoisted.startGoogleChatMonitor.mockResolvedValue(vi.fn());
370+
371+
const { abort, patches, task, isSettled } = startAccountAndTrackLifecycle({
372+
startAccount: startGoogleChatGatewayAccount,
373+
account: buildAccount(),
374+
});
375+
await expectPendingUntilAbort({
376+
waitForStarted: waitForGoogleChatMonitorStarted,
377+
isSettled,
378+
abort,
379+
task,
380+
});
381+
382+
expectLifecyclePatch(patches, { running: true, webhookPath: "/googlechat" });
383+
expect(patches.some((patch) => patch.lifecycle === "blocked")).toBe(false);
384+
});
385+
386+
it("reports a blocked lifecycle when the configured webhookUrl resolves to no path", async () => {
387+
hoisted.startGoogleChatMonitor.mockResolvedValue(vi.fn());
388+
const account = buildAccount();
389+
390+
const { abort, patches, task, isSettled } = startAccountAndTrackLifecycle({
391+
startAccount: startGoogleChatGatewayAccount,
392+
account: {
393+
...account,
394+
config: {
395+
...account.config,
396+
webhookPath: undefined,
397+
webhookUrl: "chat.example.com/googlechat",
398+
},
399+
},
400+
});
401+
await expectPendingUntilAbort({
402+
waitForStarted: waitForGoogleChatMonitorStarted,
403+
isSettled,
404+
abort,
405+
task,
406+
});
407+
408+
const startPatch = patches.find((patch) => patch.running === true);
409+
expect(startPatch).toBeDefined();
410+
expect(startPatch?.lifecycle).toBe("blocked");
411+
expect(startPatch?.lastError).toContain("webhookUrl");
412+
// The account must not advertise a route the monitor never registered.
413+
expect(startPatch?.webhookPath).toBeUndefined();
414+
});
415+
416+
it("clears a previously published webhook path when a restart resolves none", async () => {
417+
hoisted.startGoogleChatMonitor.mockResolvedValue(vi.fn());
418+
const account = buildAccount();
419+
const resolvable = {
420+
...account,
421+
config: {
422+
...account.config,
423+
webhookPath: undefined,
424+
webhookUrl: "https://chat.example.com/gc-inbound",
425+
},
426+
};
427+
const firstAbort = new AbortController();
428+
// One context, so both starts write through the same status snapshot the way
429+
// the gateway's runtime store patch-merges successive plugin patches.
430+
const ctx = createStartAccountContext({
431+
account: resolvable,
432+
abortSignal: firstAbort.signal,
433+
});
434+
const firstRun = startGoogleChatGatewayAccount(ctx);
435+
await waitForGoogleChatMonitorStarted();
436+
expect(ctx.getStatus().webhookPath).toBe("/gc-inbound");
437+
firstAbort.abort();
438+
await firstRun;
439+
440+
hoisted.startGoogleChatMonitor.mockClear();
441+
const secondAbort = new AbortController();
442+
const secondRun = startGoogleChatGatewayAccount({
443+
...ctx,
444+
account: {
445+
...resolvable,
446+
config: { ...resolvable.config, webhookUrl: "chat.example.com/gc-inbound" },
447+
},
448+
abortSignal: secondAbort.signal,
449+
});
450+
await waitForGoogleChatMonitorStarted();
451+
452+
const restarted = ctx.getStatus();
453+
expect(restarted.lifecycle).toBe("blocked");
454+
expect(restarted.webhookPath).toBeUndefined();
455+
secondAbort.abort();
456+
await secondRun;
457+
});
458+
367459
it("clears running status when monitor startup fails", async () => {
368460
hoisted.startGoogleChatMonitor.mockRejectedValue(new Error("webhook bind failed"));
369461
const patches: ChannelAccountSnapshot[] = [];

0 commit comments

Comments
 (0)