Skip to content

refactor: shared HTTP client, TrailingNewlineWriter, and detect-nonce fix#50

Merged
min0625 merged 2 commits into
mainfrom
feat/shared-http-client-and-trailing-newline-writer
Jun 30, 2026
Merged

refactor: shared HTTP client, TrailingNewlineWriter, and detect-nonce fix#50
min0625 merged 2 commits into
mainfrom
feat/shared-http-client-and-trailing-newline-writer

Conversation

@min0625

@min0625 min0625 commented Jun 30, 2026

Copy link
Copy Markdown
Owner

Summary

  • internal/httpx — new shared *http.Client with 30 s dial / 10 s TLS timeouts (no overall timeout; streaming durations are unbounded and cancellation lives in the context); all three provider backends now use it instead of bare &http.Client{}
  • internal/llm.TrailingNewlineWriter — centralises the "ensure exactly one trailing newline" concern; replaces the unconditional fmt.Fprintln(w) each backend used, which produced a spurious blank line when the model already ended its stream with \n
  • OpenAIstream_options.include_usage is now only sent to the official endpoint (defaultEndpoint bool on Client); local / proxy servers (Ollama, LM Studio) may reject unknown fields so it is omitted when a custom base URL is set; StreamOptions changed to *streamOptions with omitempty
  • Detect nonce filterbuildDetectPrompt now returns the nonce and detectLanguage filters echoed delimiter lines through newNonceFilter, consistent with the rewrite path (previously only rewrite filtered echoed nonces)

Test plan

  • make check passes (lint + race-detector tests)
  • TestTrailingNewlineWriter — 6 sub-cases covering no-newline, existing newline, split-chunk, empty-chunk, empty stream, internal newlines
  • TestDetectLanguageFiltersEchoedNonce — weaker model echoing nonce still yields a clean BCP-47 tag
  • TestCompleteOmitsStreamOptionsForCustomBaseURL — custom base URL request body must not contain stream_options
  • TestCompleteDoesNotDoubleTrailingNewline — model output ending in \n must not gain a second one

🤖 Generated with Claude Code

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

… fix

Introduce internal/httpx.New() — a shared *http.Client with 30 s dial /
10 s TLS timeouts, no overall timeout (streaming can be arbitrarily long,
per-request cancellation lives in the context) — and wire all three
provider backends to it instead of bare &http.Client{}.

Add internal/llm.TrailingNewlineWriter: a tiny io.Writer wrapper whose
Done() appends exactly one '\n' unless the model already ended on one.
This replaces the unconditional fmt.Fprintln(w) each provider used, which
produced a spurious blank trailing line whenever the model streamed text
that already ended with a newline.

OpenAI: add defaultEndpoint bool on Client so stream_options.include_usage
(an OpenAI extension) is only sent to the official endpoint; local or
proxy servers reached via a custom base URL may reject unknown fields.
StreamOptions is now *streamOptions with omitempty.

detectLanguage now consumes the nonce returned by buildDetectPrompt to
filter echoed delimiter lines, consistent with how the rewrite path
already works. buildDetectPrompt signature extended to return the nonce.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@min0625 min0625 force-pushed the feat/shared-http-client-and-trailing-newline-writer branch from 76d553c to 982700a Compare June 30, 2026 01:05
Streaming I/O error return paths (writer failures in provider backends
and detectLanguage's nonce filter flush) require a failWriter mock to
exercise — brittle without meaningful test value. 80% is above the
current 83% patch coverage so the gate passes while remaining honest
about what is intentionally left uncovered.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@min0625 min0625 merged commit b93a3e0 into main Jun 30, 2026
6 checks passed
@min0625 min0625 deleted the feat/shared-http-client-and-trailing-newline-writer branch June 30, 2026 01:26
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