fix(webfetch): avoid rewriting successful redirect content

This commit is contained in:
YeonGyu-Kim
2026-03-24 23:59:57 +09:00
parent af301ab29a
commit 971912e065
2 changed files with 20 additions and 2 deletions
+5 -1
View File
@@ -56,6 +56,10 @@ function isRedirectLoopError(output: string): boolean {
return WEBFETCH_REDIRECT_ERROR_PATTERNS.some((pattern) => pattern.test(output))
}
function isToolErrorOutput(output: string): boolean {
return output.trimStart().toLowerCase().startsWith("error:")
}
function buildRedirectLimitMessage(url?: string): string {
const suffix = url ? ` for ${url}` : ""
return `Error: WebFetch failed: exceeded maximum redirects (${MAX_WEBFETCH_REDIRECTS})${suffix}`
@@ -111,7 +115,7 @@ export function createWebFetchRedirectGuardHook(_ctx: PluginInput) {
return
}
if (isRedirectLoopError(output.output)) {
if (isToolErrorOutput(output.output) && isRedirectLoopError(output.output)) {
output.output = buildRedirectLimitMessage()
}
},
@@ -49,7 +49,7 @@ function createFetchMock(
implementation: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
): typeof fetch {
return Object.assign(implementation, {
preconnect: originalFetch.preconnect,
preconnect: Reflect.get(originalFetch, "preconnect"),
})
}
@@ -152,6 +152,20 @@ describe("createWebFetchRedirectGuardHook", () => {
)
})
})
describe("#when successful fetched content mentions redirect loops", () => {
it("#then should keep the content unchanged", async () => {
const hook = createWebFetchRedirectGuardHook({} as never)
const input = createInput()
const output = createAfterOutput("This page explains why browsers hit too many redirects in some setups.")
await hook["tool.execute.after"](input, output)
expect(output.output).toBe(
"This page explains why browsers hit too many redirects in some setups.",
)
})
})
})
describe("#given a non-webfetch tool", () => {