Refactor: Extract duplicate log metrics finalization logic - #4105
Merged
Conversation
- Add FinalizeToolMetrics() in metrics.go for full finalization (Copilot, Codex) - Add FinalizeToolCallsAndSequence() in metrics.go for tool call finalization (Claude) - Update Copilot engine to use FinalizeToolMetrics() - Update Codex engine to use FinalizeToolMetrics() - Update Claude engine to use FinalizeToolCallsAndSequence() - Remove unused sort import from claude_logs.go - Add comprehensive tests for both helper functions - All existing tests pass without modification Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor duplicate log metrics finalization logic
Refactor: Extract duplicate log metrics finalization logic
Nov 15, 2025
pelikhan
approved these changes
Nov 16, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors duplicate log metrics finalization logic that was repeated across three engine parsers (Copilot, Codex, and Claude). The refactoring extracts ~15 lines of identical finalization code (appending sequences, converting tool call maps to sorted slices, counting errors via pattern matching) into two reusable helper functions in metrics.go.
Key changes:
- Adds
FinalizeToolMetrics()for full finalization (used by Copilot and Codex engines) - Adds
FinalizeToolCallsAndSequence()as a lightweight variant (used by Claude engine which manages tokens/turns separately) - Replaces 25-28 lines per engine with single function calls
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/workflow/metrics.go | Adds two new helper functions for metrics finalization and imports sort package |
| pkg/workflow/metrics_test.go | Adds comprehensive test coverage with 9 test cases across 2 test functions |
| pkg/workflow/copilot_engine.go | Replaces 25 lines of finalization logic with single FinalizeToolMetrics() call |
| pkg/workflow/codex_engine.go | Replaces 25 lines of finalization logic with single FinalizeToolMetrics() call |
| pkg/workflow/claude_logs.go | Replaces 18 lines of finalization logic with FinalizeToolCallsAndSequence() call and removes unused sort import |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
Three engine parsers (Copilot, Codex, Claude) duplicated ~15 lines of metrics finalization: appending sequences, converting tool call maps to sorted slices, counting errors via pattern matching.
Changes
Added
FinalizeToolMetrics()inmetrics.goHandles full finalization (sequences, token usage, turns, sorting, error patterns). Used by Copilot and Codex.
Added
FinalizeToolCallsAndSequence()inmetrics.goLightweight variant for Claude, which extracts token/turns from result entries rather than accumulating during parse.
Updated engine parsers
Replaced 25-28 lines per engine with single function calls.
Before
After
Impact
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.