Skip to content

[WIP] Consolidate duplicate implementations of extractErrorMessage#5683

Merged
pelikhan merged 2 commits intomainfrom
copilot/consolidate-extract-error-message
Dec 6, 2025
Merged

[WIP] Consolidate duplicate implementations of extractErrorMessage#5683
pelikhan merged 2 commits intomainfrom
copilot/consolidate-extract-error-message

Conversation

Copy link
Contributor

Copilot AI commented Dec 6, 2025

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>[plan] Consolidate extractErrorMessage() duplicate implementations</issue_title>
<issue_description>## Objective

Unify two nearly identical extractErrorMessage() functions into a single, optimized implementation.

Problem

Two duplicate implementations exist with different performance characteristics:

Location 1: pkg/workflow/metrics.go:462 (optimized - uses pre-compiled regex)
Location 2: pkg/cli/copilot_agent.go:297 (inefficient - compiles regex inline)

Approach

  1. Create a new shared utility file: pkg/logger/error_formatting.go
  2. Implement unified function using pre-compiled regex patterns (metrics.go version)
  3. Export the function as ExtractErrorMessage(line string) string
  4. Update both pkg/workflow/metrics.go and pkg/cli/copilot_agent.go to import and use the new function
  5. Remove the duplicate local implementations

Files to Modify

  • Create: pkg/logger/error_formatting.go
  • Update: pkg/workflow/metrics.go (use new function, remove duplicate)
  • Update: pkg/cli/copilot_agent.go (use new function, remove duplicate)

Implementation Details

Use the optimized version from metrics.go with pre-compiled patterns:

var (
    timestampPattern1 = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}[T\s]\d{2}:\d{2}:\d{2}(\.\d+)?([+-]\d{2}:\d{2}|Z)?\s*`)
    timestampPattern2 = regexp.MustCompile(`^\[\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\]\s*`)
    timestampPattern3 = regexp.MustCompile(`^\d{2}:\d{2}:\d{2}\s*`)
    logLevelPattern   = regexp.MustCompile(`^(ERROR|WARN|WARNING|INFO|DEBUG):\s*`)
)

func ExtractErrorMessage(line string) string {
    cleaned := timestampPattern1.ReplaceAllString(line, "")
    cleaned = timestampPattern2.ReplaceAllString(cleaned, "")
    cleaned = timestampPattern3.ReplaceAllString(cleaned, "")
    cleaned = logLevelPattern.ReplaceAllString(cleaned, "")
    return strings.TrimSpace(cleaned)
}

Acceptance Criteria

  • New pkg/logger/error_formatting.go file created with unified function
  • Both calling sites updated to use new function
  • Duplicate implementations removed
  • All tests pass (make test)
  • Linting passes (make lint)
  • No functionality changes (behavior identical to before)

Estimated Effort

1-2 hours
Related to #5677

AI generated by Plan Command for #5506

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ErrorMessage()

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI requested a review from mnkiefer December 6, 2025 19:38
@pelikhan pelikhan marked this pull request as ready for review December 6, 2025 20:12
@pelikhan pelikhan merged commit 587b0e9 into main Dec 6, 2025
6 checks passed
@pelikhan pelikhan deleted the copilot/consolidate-extract-error-message branch December 6, 2025 20:13
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.

[plan] Consolidate extractErrorMessage() duplicate implementations

3 participants