Skip to content

Commit b0f4dde

Browse files
committed
fix(googlechat): drop invalid thread resource names before send
Reply routing can hand sendGoogleChatMessage a `thread` that is not a valid `spaces/{space}/threads/{thread}` resource name — a bare id, a `spaces/{space}/messages/{message}` name, or a thread from a different (or wrongly-cased) space. The Chat API rejects the whole request with `400 INVALID_ARGUMENT`, so the reply is never delivered even though the agent already produced it (and did any side effects). Guard the send so the `thread` field and the messageReplyOption fallback are applied only when the thread is a well-formed name belonging to the target space; otherwise post to the space as a new thread. Revives the approach of #28153 (auto-closed as stale) and addresses the failure mode behind #64313.
1 parent 29577fb commit b0f4dde

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎extensions/googlechat/src/api.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ async function fetchBuffer(
178178
});
179179
}
180180

181+
/**
182+
* A Google Chat `thread` must be a `spaces/{space}/threads/{thread}` resource
183+
* name that belongs to the target space. Reply routing sometimes yields other
184+
* shapes — a bare id, a `spaces/{space}/messages/{message}` name, or a thread
185+
* from a different (or wrongly-cased) space — and passing any of those makes the
186+
* Chat API reject the whole send with `400 INVALID_ARGUMENT`. Accept only a
187+
* well-formed, same-space thread name; callers drop the rest so the message
188+
* still delivers to the space (as a new thread) instead of failing outright.
189+
*/
190+
function isUsableGoogleChatThreadName(thread: string, space: string): boolean {
191+
return /^spaces\/[^/]+\/threads\/[^/]+$/.test(thread) && thread.startsWith(`${space}/threads/`);
192+
}
193+
181194
export async function sendGoogleChatMessage(params: {
182195
account: ResolvedGoogleChatAccount;
183196
space: string;
@@ -186,6 +199,7 @@ export async function sendGoogleChatMessage(params: {
186199
cardsV2?: GoogleChatCardV2[];
187200
}): Promise<{ messageName?: string; threadName?: string } | null> {
188201
const { account, space, text, thread, cardsV2 } = params;
202+
const usableThread = thread && isUsableGoogleChatThreadName(thread, space) ? thread : undefined;
189203
if (
190204
text &&
191205
(!cardsV2 || cardsV2.length === 0) &&
@@ -200,11 +214,11 @@ export async function sendGoogleChatMessage(params: {
200214
if (cardsV2 && cardsV2.length > 0) {
201215
body.cardsV2 = cardsV2;
202216
}
203-
if (thread) {
204-
body.thread = { name: thread };
217+
if (usableThread) {
218+
body.thread = { name: usableThread };
205219
}
206220
const urlObj = new URL(`${CHAT_API_BASE}/${space}/messages`);
207-
if (thread) {
221+
if (usableThread) {
208222
urlObj.searchParams.set("messageReplyOption", "REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD");
209223
}
210224
const url = urlObj.toString();

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,58 @@ describe("sendGoogleChatMessage", () => {
568568
expect(String(url)).not.toContain("messageReplyOption=");
569569
});
570570

571+
it.each([
572+
["a bare id", "113887189178345237288721356"],
573+
["a thread key without prefix", "pytxeqyhqck"],
574+
["a message resource name", "spaces/AAA/messages/1720896000000.000000"],
575+
["a space resource name", "spaces/AAA"],
576+
["a thread from a different space", "spaces/BBB/threads/xyz"],
577+
])(
578+
"drops an invalid thread resource name (%s) and posts to the space",
579+
async (_label, badThread) => {
580+
const fetchMock = stubSuccessfulSend("spaces/AAA/messages/126");
581+
582+
const result = await sendGoogleChatMessage({
583+
account,
584+
space: "spaces/AAA",
585+
text: "hello",
586+
thread: badThread,
587+
});
588+
589+
const url = mockCallArg(fetchMock);
590+
const init = mockCallArg(fetchMock, 0, 1) as RequestInit | undefined;
591+
// Invalid thread must not be forwarded, and the reply option must be omitted
592+
// so the Chat API accepts the send instead of returning 400 INVALID_ARGUMENT.
593+
expect(String(url)).not.toContain("messageReplyOption=");
594+
if (typeof init?.body !== "string") {
595+
throw new Error("Expected Google Chat request body");
596+
}
597+
const body = JSON.parse(init.body) as { thread?: unknown };
598+
expect(body.thread).toBeUndefined();
599+
expect(result).toEqual({ messageName: "spaces/AAA/messages/126" });
600+
},
601+
);
602+
603+
it("keeps a valid same-space thread resource name", async () => {
604+
const fetchMock = stubSuccessfulSend("spaces/AAA/messages/127", "spaces/AAA/threads/xyz");
605+
606+
await sendGoogleChatMessage({
607+
account,
608+
space: "spaces/AAA",
609+
text: "hello",
610+
thread: "spaces/AAA/threads/xyz",
611+
});
612+
613+
const url = mockCallArg(fetchMock);
614+
const init = mockCallArg(fetchMock, 0, 1) as RequestInit | undefined;
615+
expect(String(url)).toContain("messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD");
616+
if (typeof init?.body !== "string") {
617+
throw new Error("Expected Google Chat request body");
618+
}
619+
const body = JSON.parse(init.body) as { thread?: { name?: unknown } };
620+
expect(body.thread?.name).toBe("spaces/AAA/threads/xyz");
621+
});
622+
571623
it("sends cardsV2 with the text fallback", async () => {
572624
const fetchMock = stubSuccessfulSend("spaces/AAA/messages/125");
573625
const cardsV2 = [

0 commit comments

Comments
 (0)