Skip to content

fix(webfetch): guard redirect loops in built-in flow#2810

Merged
code-yeongyu merged 2 commits intodevfrom
fix/webfetch-redirect-loop
Mar 24, 2026
Merged

fix(webfetch): guard redirect loops in built-in flow#2810
code-yeongyu merged 2 commits intodevfrom
fix/webfetch-redirect-loop

Conversation

@code-yeongyu
Copy link
Copy Markdown
Owner

@code-yeongyu code-yeongyu commented Mar 24, 2026

Problem

The webfetch tool crashes with 'The response redirected too many times' when encountering redirect loops. No graceful handling — the error propagates raw to the user.

Fix

  • Added a redirect-loop guard hook that intercepts webfetch tool execution
  • Implements configurable max redirect limit with clear error messages
  • Avoids rewriting content on successful redirects
  • Added test coverage for redirect loop detection

Verification

  • bun run typecheck — pass
  • bun run build — pass
  • bun test src/hooks/webfetch-redirect-guard — pass
  • review-work: all 5 lanes passed (goal, QA, code quality, security, context mining)

Summary by cubic

Prevents webfetch crashes on redirect loops by adding a guard that pre-resolves redirects and normalizes errors. Users now get a clear message instead of raw “too many redirects,” and successful content stays unchanged.

  • Bug Fixes
    • Added webfetch-redirect-guard hook to pre-resolve redirects (manual follow) and cap at 10.
    • Rewrites “too many redirects” errors to a clear message, including the original URL when available.
    • Preserves successful outputs even if they mention redirects (no false rewrites).
    • Respects format and timeout args with sane headers and timeout caps.
    • Integrated into tool.execute.before and tool.execute.after; hook is toggleable via config schema.
    • Added tests: single redirect, relative locations, loop limit, error normalization, and non-webfetch no-op.

Written for commit 971912e. Summary will update on new commits.

@code-yeongyu code-yeongyu merged commit 0b77e2d into dev Mar 24, 2026
5 checks passed
@code-yeongyu code-yeongyu deleted the fix/webfetch-redirect-loop branch March 24, 2026 15:40
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 10 files

Confidence score: 2/5

  • There is a high-confidence, high-severity compatibility risk in src/plugin/tool-execute-after.ts, so merge risk is elevated rather than routine.
  • The redirect guard added in tool.execute.after is reported to not catch webfetch/native fetch redirect-loop exceptions in the Opencode SDK, which can leave crashes unhandled and user-facing failures unresolved.
  • Given the concrete behavior mismatch (severity 8/10, confidence 10/10), this is likely to cause a real regression unless addressed before merge.
  • Pay close attention to src/plugin/tool-execute-after.ts - redirect-loop exceptions may bypass the new guard and still crash tool execution.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/plugin/tool-execute-after.ts">

<violation number="1" location="src/plugin/tool-execute-after.ts:128">
P1: Custom agent: **Opencode Compatibility**

Adding the redirect guard to `tool.execute.after` will not intercept `webfetch` crashes. In the Opencode SDK, tool execution exceptions (like native `fetch` redirect loop errors) cause the wrapper to throw, bypassing the `tool.execute.after` hook entirely.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

await hooks.taskResumeInfo?.["tool.execute.after"]?.(input, output)
await hooks.readImageResizer?.["tool.execute.after"]?.(input, output)
await hooks.hashlineReadEnhancer?.["tool.execute.after"]?.(input, output)
await hooks.webfetchRedirectGuard?.["tool.execute.after"]?.(input, output)
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Custom agent: Opencode Compatibility

Adding the redirect guard to tool.execute.after will not intercept webfetch crashes. In the Opencode SDK, tool execution exceptions (like native fetch redirect loop errors) cause the wrapper to throw, bypassing the tool.execute.after hook entirely.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/plugin/tool-execute-after.ts, line 128:

<comment>Adding the redirect guard to `tool.execute.after` will not intercept `webfetch` crashes. In the Opencode SDK, tool execution exceptions (like native `fetch` redirect loop errors) cause the wrapper to throw, bypassing the `tool.execute.after` hook entirely.</comment>

<file context>
@@ -125,6 +125,7 @@ export function createToolExecuteAfterHandler(args: {
       await hooks.taskResumeInfo?.["tool.execute.after"]?.(input, output)
       await hooks.readImageResizer?.["tool.execute.after"]?.(input, output)
       await hooks.hashlineReadEnhancer?.["tool.execute.after"]?.(input, output)
+      await hooks.webfetchRedirectGuard?.["tool.execute.after"]?.(input, output)
       await hooks.jsonErrorRecovery?.["tool.execute.after"]?.(input, output)
     }
</file context>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant