Skip to content

[Repo Assist] refactor: extract SortedSetKeys and atomicWriteFile to eliminate code duplication#8042

Merged
lpcox merged 1 commit into
mainfrom
repo-assist/improve-dedup-strutil-8022-3ece5b714bd90f6f
Jun 24, 2026
Merged

[Repo Assist] refactor: extract SortedSetKeys and atomicWriteFile to eliminate code duplication#8042
lpcox merged 1 commit into
mainfrom
repo-assist/improve-dedup-strutil-8022-3ece5b714bd90f6f

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This is an automated PR from Repo Assist.

Addresses duplicate code patterns identified in #8021, specifically sub-issues #8022 and #8023.

What Changed

1. strutil.SortedSetKeys — new utility (closes #8022)

Added SortedSetKeys(set map[string]struct{}) []string to internal/strutil/strutil.go. This eliminates the identical 5-line map-to-sorted-slice block that appeared in three places:

  • internal/urlutil/domains.go (×2: ExtractURLDomainsFromValue, ExtractURLDomains)
  • internal/logger/observed_url_domains_logger.go (×1: writeToFile)

2. atomicWriteFile — shared logger helper + bug fix (closes #8023)

Added atomicWriteFile(filePath string, data []byte, perm os.FileMode) error to internal/logger/common.go. This helper:

  • Writes data atomically via temp-file + rename
  • Includes the os.IsNotExist guard on cleanup that was already present in observed_url_domains_logger.go but missing in tools_logger.go (latent bug fixed)
  • Logs a WARNING on unexpected cleanup failures

Both ToolsLogger.writeToFile and ObservedURLDomainsLogger.writeToFile now delegate to this helper.

Root Cause

The writeToFile methods in tools_logger.go and observed_url_domains_logger.go were written independently and diverged in error handling. The os.Remove call in tools_logger.go silently discarded cleanup errors, while observed_url_domains_logger.go correctly guarded against os.IsNotExist. atomicWriteFile adopts the correct variant for both callers.

Impact

Metric Before After
Duplicated map→slice blocks 3 0
Duplicated atomic-write blocks 2 0
Lines removed (net) −18
Latent bug (missing IsNotExist guard) present fixed

Test Status

⚠️ Build infrastructure (proxy.golang.org) is blocked in this environment. All 6 changed files pass gofmt -e with no parse errors. CI will run the full test suite including TestWriteToFile_RenameFails which exercises the cleanup path.

Existing tests that cover the changed paths:

  • TestWriteToFile_RenameFails — verifies temp file is cleaned up on rename failure
  • TestWriteToFile_WriteFileFails — verifies error propagation when write fails
  • TestWriteToFile_Success — verifies normal write path
  • TestSortedSetKeys (new) — 4 subtests covering sorted output, empty/nil maps, single element

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • proxy.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "proxy.golang.org"

See Network Configuration for more information.

Generated by Repo Assist · 282.1 AIC · ⊞ 12.2K ·
Comment /repo-assist to run again

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@851905c06e905bf362a9f6cc54f912e3df747d55

- Add strutil.SortedSetKeys to eliminate the duplicated map-to-sorted-
  slice pattern found in 3 locations (closes #8022)
- Add logger.atomicWriteFile to unify the atomic temp-file+rename
  pattern used by ToolsLogger and ObservedURLDomainsLogger (closes #8023)
- Fix latent bug in tools_logger.go: os.Remove on rename failure now
  uses the os.IsNotExist guard that was already present in
  observed_url_domains_logger.go
- Replace 3 call sites in urlutil/domains.go and logger/ with
  strutil.SortedSetKeys, removing 18 lines of duplicated code

Both improvements address patterns identified in duplicate code
analysis report #8021.

⚠️ Test Status: Build/test infrastructure (proxy.golang.org) is
blocked in this environment; tests are expected to pass in CI.
Code passes gofmt and all 6 changed files parse without errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review June 24, 2026 14:29
Copilot AI review requested due to automatic review settings June 24, 2026 14:29

Copilot AI 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.

Pull request overview

Refactors duplicated “set → sorted slice” and “atomic write (temp + rename)” patterns into shared helpers to reduce divergence risk and keep behavior consistent across internal/urlutil, internal/strutil, and internal/logger.

Changes:

  • Added strutil.SortedSetKeys(map[string]struct{}) []string and replaced three call sites that previously hand-rolled map key sorting.
  • Added logger.atomicWriteFile(filePath, data, perm) and updated ToolsLogger + ObservedURLDomainsLogger to delegate atomic file writes through it.
  • Added unit tests for SortedSetKeys.
Show a summary per file
File Description
internal/urlutil/domains.go Replaces duplicated domain-set key sorting with strutil.SortedSetKeys.
internal/strutil/strutil.go Introduces SortedSetKeys helper for consistent, reusable sorted key extraction.
internal/strutil/util_test.go Adds TestSortedSetKeys covering sorting + empty/nil behavior.
internal/logger/common.go Adds shared atomicWriteFile helper to unify atomic-write + cleanup behavior.
internal/logger/tools_logger.go Delegates ToolsLogger.writeToFile atomic write logic to atomicWriteFile.
internal/logger/observed_url_domains_logger.go Delegates both domain-set sorting and atomic write logic to shared helpers.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 6/6 changed files
  • Comments generated: 0

@lpcox lpcox merged commit d2f024c into main Jun 24, 2026
24 checks passed
@lpcox lpcox deleted the repo-assist/improve-dedup-strutil-8022-3ece5b714bd90f6f branch June 24, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants