Skip to content

Commit e26bae5

Browse files
authored
fix(xai): fence cancelled x_search cache writes (#123996)
Prevent a completed standalone x_search request from building or caching a result after caller cancellation. Preserve the exact abort reason and force identical retries back through the provider request path.\n\nFixes #123964
1 parent 4e64649 commit e26bae5

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎extensions/xai/x-search.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,29 @@ describe("xai x_search tool", () => {
276276
expect(mockFetch).not.toHaveBeenCalled();
277277
});
278278

279+
it("does not cache an X search result completed after caller cancellation", async () => {
280+
const controller = new AbortController();
281+
const reason = new Error("operator cancelled X search after response");
282+
const mockFetch = vi
283+
.fn()
284+
.mockImplementationOnce(async () => {
285+
controller.abort(reason);
286+
return jsonResponse({ output_text: "Cancelled X answer", citations: [] });
287+
})
288+
.mockResolvedValueOnce(jsonResponse({ output_text: "Recovered X answer", citations: [] }));
289+
global.fetch = withFetchPreconnect(mockFetch);
290+
const tool = createConfiguredXSearchTool();
291+
const query = "unique standalone x_search late-cancel cache regression";
292+
293+
await expect(tool.execute("xai-late-cancel", { query }, controller.signal)).rejects.toBe(
294+
reason,
295+
);
296+
const recovered = await tool.execute("xai-late-cancel-retry", { query });
297+
298+
expect(mockFetch).toHaveBeenCalledTimes(2);
299+
expect((recovered.details as { content?: string }).content).toContain("Recovered X answer");
300+
});
301+
279302
it("uses the xAI Responses x_search tool with structured filters", async () => {
280303
const mockFetch = installXSearchFetch();
281304
const tool = createConfiguredXSearchTool({ xSearch: { maxTurns: 2 } });

‎extensions/xai/x-search.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export function createXSearchTool(options?: {
242242
options: xSearchOptions,
243243
...(signal ? { signal } : {}),
244244
});
245+
signal?.throwIfAborted();
245246
const payload = buildXaiXSearchPayload({
246247
query,
247248
model,

0 commit comments

Comments
 (0)