Skip to content

[Repo Assist] refactor(logger): eliminate withLock duplication across 4 logger types#2920

Merged
lpcox merged 1 commit intomainfrom
repo-assist/fix-withlock-duplication-2026-03-31-98d19bba53c613dd
Mar 31, 2026
Merged

[Repo Assist] refactor(logger): eliminate withLock duplication across 4 logger types#2920
lpcox merged 1 commit intomainfrom
repo-assist/fix-withlock-duplication-2026-03-31-98d19bba53c613dd

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Eliminates the four identical withLock method bodies across FileLogger, JSONLLogger, MarkdownLogger, and ToolsLogger by adding a single package-level withMutexLock helper to global_helpers.go, and having each per-type method delegate to it.

Closes #2908

Root Cause

Each of the four logger types contained a verbatim copy of the same 3-line lock/unlock pattern:

func (fl *FileLogger) withLock(fn func() error) error {
    fl.mu.Lock()
    defer fl.mu.Unlock()
    return fn()
}

Any future change to the locking strategy (tracing, timeout, different mutex type) would have required four identical edits.

Fix

Added one canonical helper to global_helpers.go:

// withMutexLock acquires mu, calls fn, and releases mu.
func withMutexLock(mu *sync.Mutex, fn func() error) error {
    mu.Lock()
    defer mu.Unlock()
    return fn()
}

Each per-type withLock now delegates in a single line:

func (fl *FileLogger) withLock(fn func() error) error {
    return withMutexLock(&fl.mu, fn)
}

Trade-offs

  • Zero behaviour change: same mutex, same defer, same return path
  • All four mu fields are sync.Mutex (verified), so the *sync.Mutex parameter type is exact
  • The per-type withLock methods are kept for readability at call sites; callers don't need to change
  • Consistent with the existing generic patterns already in global_helpers.go (initGlobalLogger, closeGlobalLogger, withGlobalLogger)

Test Status

⚠️ Infrastructure limitation: This environment has Go 1.24.13 but go.mod requires Go ≥ 1.25.0, and proxy.golang.org is blocked by the network firewall. Tests cannot be run locally.

The change has been reviewed manually:

  • All five files compile independently (no new imports needed — sync already imported everywhere)
  • withMutexLock takes *sync.Mutex; all four logger mu fields are exactly sync.Mutex
  • Behaviour is byte-for-byte equivalent to the original implementations ✓
  • global_helpers.go already imports sync, so no import changes needed ✓

CI will validate build and tests on merge.

Generated by Repo Assist ·

To install this agentic workflow, run

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

Add a package-level withMutexLock helper to global_helpers.go and have
each logger's withLock method delegate to it, eliminating 4 identical
lock/unlock bodies.

Before: each of FileLogger, JSONLLogger, MarkdownLogger, and ToolsLogger
contained an identical withLock implementation — 3 lines each, 12 total.

After: each delegates to withMutexLock in one line; the canonical
implementation lives in one place.

Zero behaviour change: same mutex, same defer, same return path.

Closes #2908

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review March 31, 2026 18:07
Copilot AI review requested due to automatic review settings March 31, 2026 18:07
@lpcox lpcox merged commit a692d0a into main Mar 31, 2026
5 checks passed
@lpcox lpcox deleted the repo-assist/fix-withlock-duplication-2026-03-31-98d19bba53c613dd branch March 31, 2026 18:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the internal/logger package to remove duplicated mutex lock/unlock boilerplate by centralizing the common pattern in a single helper, while keeping the existing per-logger withLock methods unchanged for callers.

Changes:

  • Added a package-level withMutexLock(*sync.Mutex, func() error) error helper in global_helpers.go.
  • Updated withLock implementations in FileLogger, JSONLLogger, MarkdownLogger, and ToolsLogger to delegate to the shared helper.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/logger/global_helpers.go Adds the shared withMutexLock helper used to standardize lock/unlock behavior.
internal/logger/file_logger.go Replaces inlined mu.Lock()/Unlock() in withLock with the shared helper.
internal/logger/jsonl_logger.go Replaces inlined mu.Lock()/Unlock() in withLock with the shared helper.
internal/logger/markdown_logger.go Replaces inlined mu.Lock()/Unlock() in withLock with the shared helper.
internal/logger/tools_logger.go Replaces inlined mu.Lock()/Unlock() in withLock with the shared helper.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Pattern: Identical withLock() in Logger Types

2 participants