Skip to content

refactor: adopt modern Go stdlib helpers - #1566

Merged
lizhengfeng101 merged 3 commits into
alibaba:mainfrom
sisantaChhatoi:refactor/go-modernize
Nov 22, 2026
Merged

lizhengfeng101 merged 3 commits into
alibaba:mainfrom
sisantaChhatoi:refactor/go-modernize

Conversation

@sisantaChhatoi

@sisantaChhatoi sisantaChhatoi commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Mechanical cleanup: hand-rolled loops are replaced with the standard-library helpers available at the module's go 1.25 baseline, and no-op type conversions are dropped. The behavior should not change.

  • maps.Copy in place of copy-a-map loops (provider_tui.go, llm/client.go, llm/resolver.go, llmloop/loop.go, mcp/provider.go, tool/file_read_diff.go)
  • the min builtin in place of if end > len(x) { end = len(x) } clamps (agent, scan/batch.go, llm/keycmd.go); the package-local max in suggestdiff is dropped in favor of the builtin
  • strings.SplitSeq / bytes.SplitSeq where the split result is only iterated
  • strings.CutPrefix / CutSuffix in place of paired HasPrefix+TrimPrefix
  • slices.Contains / slices.ContainsFunc in place of linear-search loops
  • no-op conversions dropped in internal/llm: shared.ChatModel, openai.ResponsesModel and anthropic.Model are plain string aliases, and shared.FunctionParameters(...) is unnecessary because a map[string]any is assignable to it
  • range-over-int loops, and sync.WaitGroup.Go in place of Add(1) + go func(){ defer Done() }(). In triggerAsyncCompression, Go still registers the job before the goroutine starts, so the WaitBackground invariant noted in the comment still holds.

In stripMarkdownFences (both copies) only the suffix branch uses CutSuffix. The prefix branch is left as is on purpose: rewriting it with CutPrefix would feed the "```"-only cut into the no-newline path and leave json behind for input like ```json{}```. A single-line fence case is added to both test tables to guard that path.

Newer code. A third commit applies the same helpers to code added since this PR was opened, mostly internal/behaviortask, internal/chatgptauth and internal/gitcmd: strings.SplitSeq (17), range-over-int (17), sync.WaitGroup.Go (8), slices.Contains (3), maps.Copy (2), slices.Backward (1) and min (1). They come from go fix's modernizers of the same kinds, run for linux, windows and darwin, with each non-trivial rewrite read by hand. Its stringscutprefix proposal for stripMarkdownFences is left out for the reason above, and omitzero is not applied because it would change JSON output.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

  • make test passes locally
  • Manual testing (describe below)

make check also passes (license, english, gofmt, vet). The new single-line fence cases fail against the CutPrefix rewrite of stripMarkdownFences and pass with this change.

go vet also passes for GOOS=windows, and both modules build for windows and darwin on amd64 and arm64. TestPendingLoginConcurrentAccountOperations/return-login can time out under heavy CPU load (it allows one second for a full second login); it does so on main as well, and this change does not touch that path.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

AI disclosure: prepared with Claude Code (Claude Opus 5.5). It reviewed an IDE-suggested modernization diff, fixed a regression and a build break in it, and added the test cases. I reviewed every line.

Related Issues

None.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 25 selected item(s).

@Qiyuanqiii Qiyuanqiii 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.

I did another pass over the current head, this time also checking documentation impact, naming, comments, and the wording introduced by the refactor.

The stdlib substitutions still preserve the previous behavior: SplitSeq, maps.Copy, slices.Backward, the built-in min/max, range-over-int, and sync.WaitGroup.Go all match the control flow they replace here. The repository already targets Go 1.25.5, so these helpers do not raise the supported Go baseline.

I also don't see a documentation update that is required for this PR. It changes internal implementation only; CLI behavior, configuration, output formats, and user workflows stay the same.

The new and modified code comments are clear and describe the relevant invariants without over-explaining them.

No blocking findings from me. Approved.

@sisantaChhatoi

Copy link
Copy Markdown
Contributor Author

Heads up on the latest push: it's just a rebase onto current main. There was one small conflict in responses_client.go, where main added a RawUsage field next to the line this PR touches. I kept the new field and still dropped the no-op conversion. Nothing else changed. The red test/cross-compile checks stop at "Verify generated provider presets" on the self-hosted runners, same as other PRs right now; they pass locally.

@Qiyuanqiii

Copy link
Copy Markdown
Contributor

For some reason, the CI keeps failing tonight, even though GitHub claimed the issue has been fixed.

@sisantaChhatoi

Copy link
Copy Markdown
Contributor Author

Rebased onto current main again. Only an import-block conflict in file_read_diff.go, and upstream moved pool_test.go to internal/reviewtask/, so the same changes now apply there. Nothing else changed.

Replace hand-rolled loops with maps.Copy, slices.Contains(Func), min/max
builtins, strings.SplitSeq/CutPrefix/CutSuffix, range-over-int and
sync.WaitGroup.Go. No behavior change; adds a single-line fence case to
the stripMarkdownFences tests.
@sisantaChhatoi

Copy link
Copy Markdown
Contributor Author

Rebased onto current main. #1993 rewrote the compression code and removed the loop the slices.Backward commit changed, so that commit is dropped. The rest is unchanged apart from keeping the new rawOutputText field next to the dropped conversion in responses_client.go.

@lizhengfeng101 lizhengfeng101 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.

nice work

@lizhengfeng101
lizhengfeng101 merged commit d0744d3 into alibaba:main Nov 22, 2026
13 checks passed
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.

3 participants