Skip to content

fix(providers): do not retry an Ollama stream after output was emitted - #1701

Open
pcbeingused33 wants to merge 1 commit into
nextlevelbuilder:devfrom
pcbeingused33:fix/ollama-stream-no-retry-after-emit
Open

pcbeingused33 wants to merge 1 commit into
nextlevelbuilder:devfrom
pcbeingused33:fix/ollama-stream-no-retry-after-emit

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Summary

OllamaProvider.ChatStream wraps the whole client.Chat call, including the onChunk callbacks, in RetryDo. If the connection drops mid-stream (e.g. unexpected EOF, which is retryable), the request is replayed and the caller receives the already delivered text again: a stream cut after Hel reaches the caller as HelHelHel. Once a content or thinking chunk has been passed to onChunk, a stream error is now returned without retrying, the same rule CodexProvider.ChatStream already applies. Failures before any output are retried as before.

Type

  • Feature
  • Bug fix
  • Hotfix (targeting main)
  • Refactor
  • Docs
  • CI/CD

Target Branch

dev

Checklist

  • go build ./... passes
  • go build -tags sqliteonly ./... passes (if Go changes)
  • go vet ./internal/providers/ passes
  • Tests pass: go test -race ./internal/providers/
  • Web UI builds: cd ui/web && pnpm build (if UI changes) (no UI changes)
  • No hardcoded secrets or credentials
  • SQL queries use parameterized $1, $2 (no string concat) (no SQL changes)
  • New user-facing strings added to all 3 locales (en/vi/zh) (no new strings)
  • Migration version bumped in internal/upgrade/version.go (if new migration) (no migration)

Test Plan

TestOllamaChatStream_NoRetryAfterChunkEmitted uses an httptest server that sends one chunk and aborts the connection. Without the fix it sees 3 requests and HelHelHel; with it, 1 request, Hel and an error.

go test ./internal/providers/ -run TestOllamaChatStream -count=1

This PR was prepared with AI assistance (Claude Code); the change and tests were generated and run by the assistant, and the submitter has not independently reviewed the code line by line.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review: fix(providers): do not retry an Ollama stream after output was emitted

Summary: OllamaProvider.ChatStream wraps the whole client.Chat call, including onChunk delivery, in RetryDo. A mid-stream failure after chunks were delivered replays the request and duplicates the already-delivered text, so a cut after Hel reaches the caller as HelHelHel. This PR tracks whether any content/thinking chunk reached onChunk and, if so, surfaces the stream error without retrying.

Risk level: Low

Mandatory gates:

  • Duplicate / prior implementation: clear. No prior or open PR/issue implements this. The sibling CodexProvider.ChatStream already applies the same no-replay-after-emit rule at internal/providers/codex.go:152, and docs/02-providers.md:224 documents the intended streaming contract: once SSE events start flowing, no retry occurs mid-stream. This PR brings the Ollama streaming path in line with it.
  • Project standards: docs found. AGENTS.md, CLAUDE.md, CONTRIBUTING.md and docs/02-providers.md cover provider retry conventions; the change touches no i18n, SQL or migration surfaces.
  • Strategic necessity: clear value. Correctness bug fix for duplicated streamed output on a supported provider, small and well-scoped, from a repeat contributor.

Findings:

  • Critical: none
  • Important: none
  • Suggestion: none blocking. The emitted flag covers content and thinking chunks only, which matches what is delivered through onChunk since tool calls are accumulated rather than streamed, so the retry guard is correct.

Verification:

  • Diff is focused: 2 files, +47/-4 in internal/providers/ollama.go and internal/providers/ollama_test.go.
  • New test TestOllamaChatStream_NoRetryAfterChunkEmitted asserts one request, Hel delivered once, and an error returned.
  • mergeStateStatus CLEAN / MERGEABLE; CI green: go, web, release-versioning all pass.

Verdict: Approve

Merge note: merge-ready with CLEAN state and green CI, but carries 0 approvals, so this automation does not merge; a maintainer approval is required.

Posted by /ck:review-pr at 2026-11-26T13:09:00Z

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.

2 participants