Repository navigation
chore: enable the dupl linter and remove the duplication it reports - #420
Open
LarsArtmann wants to merge 4 commits into
Open
LarsArtmann wants to merge 4 commits into
LarsArtmann wants to merge 4 commits into
Conversation
Contributor
Author
|
|
LarsArtmann
marked this pull request as draft
October 16, 2026 22:16
LarsArtmann
force-pushed
the
enable-dupl-linter
branch
from
October 16, 2026 23:03
700785a to
af3e8f5
Compare
LarsArtmann
marked this pull request as ready for review
October 16, 2026 23:20
LarsArtmann
force-pushed
the
enable-dupl-linter
branch
3 times, most recently
from
October 17, 2026 01:13
64cd678 to
4d9ac1e
Compare
This was referenced Oct 17, 2026
…formats Characterization tests for the code the dedup PRs refactor next: object had no tests at all; the content codecs' provider metadata/options decode and openrouter's reasoning format labels were only pinned implicitly. Each test fails when the branch it covers is disabled.
dupl was not part of the enabled linter set, so clone detection never ran in CI. It now runs at a 75-token threshold, because the default 150 only catches whole-function copies, and the tree is clean at 75: 19 findings, 12 of them real copies. - content_json: the four text and reasoning codecs share one decoder, and the two tool-part marshals share one encoder. - object: the end-of-stream emission becomes one helper, and EmitParsedObject is exported so providers can drop their own copies. - openai: both JSON-mode streamers call EmitParsedObject instead of inlining the parse, validate, repair and emit block. - kronk: the usage fold is shared by Generate and Stream. - Two single-text-part fast paths stay mirrored, with the reason inline. New tests cover object streaming, the codec decoding and the openrouter reasoning label.
LarsArtmann
added a commit
to LarsArtmann/art-fantasy
that referenced
this pull request
Oct 24, 2026
dupl ran manually over the tests at the same 75-token threshold charmbracelet#420 uses for production code (CI still excludes tests; that is upstream's own run.tests: false). 103 findings became 69, and the test trees shrank by ~450 lines, by extracting setups rather than merging test cases.
LarsArtmann
force-pushed
the
enable-dupl-linter
branch
from
October 24, 2026 10:12
4d9ac1e to
8be8d4f
Compare
…uter
The four //nolint:dupl directives are gone: each file's user and
assistant fast paths collapse into one package-local singleTextMessage
helper that carries the role switch in a single place. The old
directives' reason ("the differing lines are the role's SDK call") was
incomplete: the warning text also names the role, which is exactly the
lockstep a shared helper encodes.
The prompttest golden matrix exercises every branch of the helper
(single text with and without the anthropic cache hint, both roles) and
is unchanged, and dupl now reports zero issues in both packages with no
suppressions. vercel and openrouter deliberately keep their own helper
copies, like their converters; the matrix makes drift visible.
💘 Generated with Crush
Assisted-by: Crush:glm-5.3
Verified what the directory rule suppressed: exactly one mirror pair in cases.go (the wav and mp3 tool-result media fixtures), which is table-driven fixture data. cases.go stays excluded for that reason; prompttest.go, the golden harness, is linted again. 💘 Generated with Crush Assisted-by: Crush:glm-5.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #421, which lands the tests pinning this refactor first; until it merges, the diff below also shows those three test files.
duplwas not in the enabled linter set, so clone detection never ran here, and the 431 clone groups in #95 have had nothing keeping new ones out. It is enabled now, at a 75-token threshold instead of the golangci-lint default of 150, because 150 only ever catches whole-function copies. The tree is clean at 75.Why 75
At the default the whole repository reports five clones, all of them 150+ token monoliths. Dropping the threshold finds the copies that actually get made, and the tree is clean down to 75:
The 19 at 75 were 12 real copies plus 7 deliberate similarities, and the ones 150 cannot see are the interesting ones: the end-of-stream emission in
object(finish part vsNoObjectGeneratedError, verbatim in both streamers), the same parse/validate/repair/emit block that openai's two JSON-mode streamers each carried, the twoToolCallPart/ToolResultPartmarshals incontent_json.go, and the kronk usage fold.What changed
.golangci.yml:duplenabled withthreshold: 75. One exclusion rule coversproviders/internal/prompttest/cases.go, the fixture corpus, where parallel case shapes are the point, not duplication; the golden harness next to it stays linted. Verified before narrowing: the directory-wide rule only ever suppressed the wav/mp3 tool-result fixture pair. (run.tests: falseis upstream's own line and stays: with tests in scope the same tree reports 255 issues, 30 of themdupl, almost all table-driven test shapes.)content_json.go: the four text/reasoning codecs (TextContent,ReasoningContent,TextPart,ReasoningPart) share oneunmarshalTextPayload, and the two tool-part marshals sharemarshalMessagePart.object/object.go: the end-of-stream emission becomesstreamEndPart, andEmitParsedObjectis exported so providers can drop their own copies of the parse/validate/repair/emit block, following the repo's existing cross-package sharing (openai.FoldDisjointReasoning,openai.GetReasoningMetadata).providers/openai/language_model.goandproviders/openai/responses_language_model.go: both JSON-mode streamers callobject.EmitParsedObjectinstead of inlining the block, which also drops an unusedreflectimport from each.providers/googlecarries a near-copy of the parse/validate/repair/emit block thatdupldoes not flag at this threshold, so it keeps its copy for now; sharing it the same way looks like a natural follow-up.providers/kronk/language_model.go: the usage fold is nowmapUsage, shared byGenerateandStream.providers/openrouter/language_model_hooks.goandproviders/vercel/language_model_hooks.gocollapse into one package-localsingleTextMessagehelper per file, so the role switch lives in one place instead of two mirrored blocks. This replaces the four//nolint:dupldirectives the branch first carried: their reason ("the differing lines are the role's SDK call") was incomplete, the warning text also names the role, and that lockstep is exactly what the helper encodes. dupl is clean in both packages with no suppressions. Each provider keeps its own helper copy, like its converter, and the prompttest matrix pins both to identical output. dupl also runs per-package, so the far larger cross-file similarity between the two converters is invisible to it regardless of threshold; unifying them is Deduplicate code across providers and core logic #95's territory, not this PR's.object/object_test.go(the package had none),content_json_test.go, andproviders/openrouter/language_model_hooks_test.go, each failing when the branch it covers is disabled. This change alone is +321/−438 — production code only, netting −117 lines — and at 75 with tests in scope, all 103duplfindings live in pre-existing test files.Behavior
No behavior changes, and that is checked rather than asserted:
lastParsedObjectbecoming a*anyparameter andusage = xbecomingreturn x.encoding/json's case-insensitive field matching; case-variant keys (Text,TEXT,PROVIDER_METADATA) were probed against the pre-change tree and decode identically, alongside a 14-payload diff (missing text, empty provider map,null, non-string text, unknown fields, unicode) in both flavours.go test ./... -count=1passes, includingproviders/openai's streaming tests.user_cache_control,assistant_cache_control, andassistant_text_onlyin the prompttest corpus exercise every branch (flat message with and without the message-level cache hint, both roles), their goldens are unchanged, and vercel and openrouter group under one golden key — identical output.Test plan
golangci-lint runwithduplat 75: 0 issues, was 19go test ./... -count=1: everything passes exceptproviders/kronk, which panics inffi.initon a missinglibffi.so.8on this machine; same panic on unmodifiedmain,go test -c ./providers/kronkcompiles, and CI'sgo test ./...runs it on three platformsgo test -raceonproviders/vercel,providers/openrouter,providers/internal/prompttest: passgolangci-lint v2.15.0, the CI pin (local default is 2.14.0): 0 issues on the touched packages
gofmt -l .,gofumpt -l .,go vet ./...: cleanbuild / build (<os>)andlintpass on CI;build / govulncheckfails onmaintoo (9 stdlib findings atgo 1.27, run 38968706300), so it is not this branchI have read
CONTRIBUTING.md.I have created a discussion that was approved by a maintainer (for new features).
💘 Generated with Crush