Skip to content

refactor(logging): rename FileBodySource.WriteTo to MergeTo; document stream cancel ownership - #177

Open
warelik wants to merge 2 commits into
kaitranntt:mainfrom
warelik:fix/vet-clean
Open

refactor(logging): rename FileBodySource.WriteTo to MergeTo; document stream cancel ownership#177
warelik wants to merge 2 commits into
kaitranntt:mainfrom
warelik:fix/vet-clean

Conversation

@warelik

@warelik warelik commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes go vet warning about FileBodySource.WriteTo shadowing the io.WriterTo interface method and documents the cancel-ownership transfer pattern in the plugin host stream bridge.

Changes

1. Rename FileBodySource.WriteToMergeTo (internal/logging)

FileBodySource.WriteTo(w io.Writer) error has a different signature than io.WriterTo.WriteTo(w io.Writer) (int64, error). This shadows the standard interface method and triggers go vet warnings. Renamed to MergeTo — semantically accurate (the method merges ordered log parts into a writer) and avoids the signature collision.

Updated all 5 call sites:

  • request_logger_body_source.go — method definition + Bytes() helper
  • request_logger_format.gowriteAPISectionWithSource, writePreformattedAPISectionWithSource
  • response_writer.gomergeFileBodySource

2. Document cancel-ownership in plugin host (internal/pluginhost)

host_callbacks.go and host_model_stream_callbacks.go both create a context.WithCancel whose cancel func transfers ownership to the stream bridge on the success path. Without a comment, go vet (and human reviewers) see cancel assigned but never called on the success path and flag it as a leak. Added _ = cancel with a comment explaining the ownership transfer.

Verification

  • go vet ./... clean
  • go build ./... clean
  • go test -count=1 ./internal/logging/... ./internal/pluginhost/... ./internal/api/middleware/...

Mirror

CPA: router-for-me/CLIProxyAPI#4943 (identical changes)

W ARELIK added 2 commits August 13, 2026 14:54
Rename the internal FileBodySource.WriteTo method to MergeTo to eliminate the inspection-time collision with the io.WriterTo interface. The previous name made the type satisfy io.WriterTo, which is a correctness hazard: callers that pass a FileBodySource where an io.WriterTo is expected could silently misroute the merge path, and the extra interface conformance invited misuse in fmt.Sprintf and other writer-to sinks.

MergeTo is behavior-identical: it merges all ordered parts into w and reports the first write error. Update the three call sites (middleware response merge, and the logging format section writers) accordingly.

Twin of CPA PR #4943 commit 74609535. Verified independently: five post-edit files byte-identical to CPA, gofmt, -race, go vet, and full test suite/build all clean.
In callHostHTTPDoStream and callHostModelExecuteStream the derived streamCtx/cancel pair is handed to the stream bridge on the success path, so the bridge owns cancellation. On error paths the local code still calls cancel() explicitly, then returns without re-calling it. Add a blank-line comment documenting that the cancel function transfers ownership to the stream bridge on success, and bind it to the blank identifier to reflect that the success path is intentionally not cancelled locally.

Comment/documentation plus a functional no-op; no behavior change. Twin of CPA PR #4943 commit 05971fbb. Verified independently: five post-edit files byte-identical to CPA, gofmt, -race, go vet, and full test suite/build all clean.
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