Skip to content

[duplicate-code] Refactor: dedupe agent_output.json extraction in logs_metrics.go - #76976

Merged
pelikhan merged 5 commits into
mainfrom
refactor/dedupe-safe-output-item-extraction-81256e46f849c011
Nov 12, 2026
Merged

pelikhan merged 5 commits into
mainfrom
refactor/dedupe-safe-output-item-extraction-81256e46f849c011

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🔍 Duplicate Code Detected: agent_output.json extraction in logs_metrics.go

Implementation: Extracted the shared locate/read/parse loop into a generic helper forEachSafeOutputItem[I] and made the existing resolveAgentOutputFile the single resolver (it now honours its previously-unused verbose parameter). Net change: +135 / −350 lines in pkg/cli/logs_metrics.go.

Summary

extractMissingToolsFromRun, extractNoopsFromRun and extractMissingDataFromRun each contained a ~110-line copy of the same logic: resolve agent_output.json (flat, legacy agent-output dir/file, recursive fallback), read it, unmarshal {"items": [...]}, then decode each item. Only the per-item struct, the type check, and the report construction differed. resolveAgentOutputFile in the same file already duplicated the resolution part (minus verbose logging).

Duplication Details
  • Severity: High (~330 duplicated lines, 3 exact copies + a 4th partial copy)
  • Locations (before change), pkg/cli/logs_metrics.go:
    • extractMissingToolsFromRun (≈ lines 281–410)
    • extractNoopsFromRun (≈ lines 413–538)
    • extractMissingDataFromRun (≈ lines 541–672)
    • resolveAgentOutputFile (≈ lines 925–950, partial)
  • Detected with a 10-line sliding-window duplicate scan over pkg/**/*.go (non-test); this was the largest cluster.

Impact Analysis

  • Maintainability: layout-resolution changes had to be made in 4 places.
  • Bug Risk: inconsistent fixes between the copies (e.g. the nested-directory handling).
  • Code Bloat: ~215 net lines removed.

Validation

  • gofmt -l pkg/cli/logs_metrics.go: clean.
  • ⚠️ Could not build/test pkg/cli in this sandbox: Go module dependencies could not be downloaded (TLS failure to proxy.golang.org), so go build ./pkg/cli, make fmt, make agent-report-progress and the existing logs_*_test.go tests were not run. Please rely on CI.
  • Instead, I ran a standalone differential harness (temp module with stubs for console, constants, fileutil, copying the old functions from HEAD and the new ones from this change) over 7 layouts (flat, legacy file, legacy dir w/ nested file, legacy dir missing nested, recursive, malformed JSON, none) × verbose on/off, with mixed missing_tool/noop/missing_data/malformed items. Returned reports were identical in all cases, and verbose stderr output was identical, with one exception below.

Behavior notes

  • The verbose "Failed to parse item" warning embeds Go's JSON error text, which names the decoded struct type (now missingToolItem.type etc. instead of an anonymous .type). No tests reference this text.
  • extractSkillActivationsFromAgentOutput (which calls resolveAgentOutputFile(runDir, verbose)) now emits the same verbose "Found ... " layout messages when verbose is true; previously the parameter was ignored.
Analysis Metadata
  • Detection Method: sliding-window duplicate scan of non-test pkg/**/*.go plus manual review (Serena CLI was available but not needed for this exact-copy finding)
  • Commit analyzed: 8c091a7
  • Other smaller clusters seen (not addressed here): pkg/cli/frontmatter_editor.go, pkg/cli/outcome_eval_generic.go, pkg/cli/pr_command.go, pkg/workflow/maintenance_workflow.go.

Generated by 🔍 Duplicate Code Detector · pi · routed: sonnet55 high · ~38 AIC · ⌖ ~2 AIC · ⊞ 7.2K · ◷

  • expires on Nov 13, 2026, 1:57 PM UTC-08:00

@github-actions github-actions Bot added automated-analysis code-quality cookie Issue Monster Loves Cookies! labels Nov 11, 2026
@pelikhan
pelikhan marked this pull request as ready for review November 11, 2026 22:00
Copilot AI dynamic review requested due to automatic review settings November 11, 2026 22:00
@github-actions

Copy link
Copy Markdown
Contributor Author

🔎 PR Code Quality Reviewer is reviewing code quality for this pull request...

@github-actions

Copy link
Copy Markdown
Contributor Author

🚀 PR Specification Reviewer has started processing this pull request

@github-actions

Copy link
Copy Markdown
Contributor Author

✂️ Ponytail Reviewer has started processing this pull request

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ Test Quality Sentinel completed test quality analysis.

Test Quality Sentinel

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Test Quality Sentinel · routed: haiku55 xhigh · ~2 AIC · ⌖ ~2 AIC · ◇ ~1 AIC · ⊞ 7.2K · ◷

🧪 Test quality analysis by Test Quality Sentinel

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer

@github-actions

This comment has been minimized.

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.

🟢 Approval recommended

No active review issues are reported for the refactor consolidating safe-output artifact resolution and parsing.

No open issues 🎉

What changed in this PR

Refactors safe-output artifact extraction in gh-aw’s log analysis code, replacing duplicated loading and parsing logic with a shared helper.

Changes:

  • Adds forEachSafeOutputItem[I] for missing-tool, noop, and missing-data reports.
  • Centralizes artifact lookup in resolveAgentOutputFile, retaining existing layout handling.
  • Enables verbose layout diagnostics in the shared resolver, including for skill-activation extraction.
File Description
pkg/​cli/​logs_metrics.go Consolidates artifact resolution and JSON parsing while retaining report-specific filtering and construction.

Review effort: Balanced (auto)

@github-actions

Copy link
Copy Markdown
Contributor Author

🏗️ Design Decision Gate — ADR Required

This PR makes significant changes to core business logic (>100 new lines) but does not have a linked Architecture Decision Record (ADR).

📄 Draft ADR committed: docs/adr/76976-consolidate-safe-output-item-extraction.md — review and complete it before merging.

🔒 This PR cannot merge until an ADR is linked in the PR body.

📋 What to do next
  1. Review the draft ADR committed to your branch — it was generated from the PR diff

  2. Complete the missing sections — add context that the AI could not infer, refine the decision rationale, and list real alternatives you considered

  3. Commit the finalized ADR to docs/adr/ on your branch

  4. Reference the ADR in this PR body by adding a line such as:

    ADR: ADR-76976: Consolidate Safe-Output Item Extraction Behind a Generic Helper (link unavailable: invalid HTTPS URL)

Once an ADR is linked in the PR body, this gate will re-run and verify the implementation matches the decision.

🧭 Scope note

The change is a refactor within existing patterns. It adds no architectural choice beyond internal consolidation, so the gate fired on the volume rule alone. If the team judges this below the ADR bar, a maintainer can say so on this PR.

🏗️ ADR gate enforced by Design Decision Gate 🏗️ · pi · routed: haiku55 xhigh · ~3 AIC · ⌖ ~2 AIC · ⊞ 8.5K · ◷
Comment /review to run again

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Skills-Based Review 🧠

Applied /codebase-design and /tdd. The refactor is behavior-preserving on every branch I traced, and I found no correctness bugs. Two follow-ups are suggested in inline comments.

📋 Key Themes & Highlights

Key Themes

  • Helper error model: forEachSafeOutputItem returns nothing and swallows read and parse failures. The extractors still return nil errors, as before, so this is not a regression. But it means the helper cannot serve extractSkillActivationsFromAgentOutput, which still duplicates the read/parse loop (around logs_metrics.go:628) and is outside this diff.
  • Test coverage: resolveAgentOutputFile now owns four layout branches, and none of the extractor tests exercises the legacy directory, nested-file, or recursive path.

Positive Highlights

  • ✅ Single resolver: resolveAgentOutputFile now honours its previously ignored verbose parameter, removing the drift between the copies.
  • ✅ Generic forEachSafeOutputItem[I] removes about 330 duplicated lines while keeping each per-item struct local.
  • ✅ go build ./pkg/cli passes and the targeted go test run for the log extractors passes.
  • ✅ Verbose messages keep their original wording, so the only visible change is the parse-error text, which the PR already notes.

Note: verification ran in this sandbox against the PR head, not in CI.

@copilot please address the review comments above.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~5 AIC · ⌖ ~2 AIC · ⊞ 6.6K
Comment /matt to run again

Comment thread pkg/cli/logs_metrics.go
// the new flattened layout first, then the legacy nested layout, and finally a
// recursive search. Returns an empty string when no file is found. When verbose
// is set, the layout that matched (or why a legacy layout was rejected) is reported.
func resolveAgentOutputFile(runDir string, verbose bool) string {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[/tdd] resolveAgentOutputFile now owns the flat, legacy-file, legacy-directory (with and without a nested file), and recursive-search branches, but no test references it or findAgentOutputFile by name, and none I found writes the directory or nested layout. Add a table test over these layouts plus malformed JSON and the no-file case, mirroring the differential harness described in the PR.

@copilot please address this.

Comment thread pkg/cli/logs_metrics.go
// legacy directory and recursive layouts), decodes each entry of its "items" array into I,
// and calls fn for every successfully decoded item. Read and parse problems are reported
// only when verbose is set, and processing continues without the failing file or item.
func forEachSafeOutputItem[I any](runDir string, run WorkflowRun, verbose bool, fn func(item I)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[/codebase-design] forEachSafeOutputItem swallows read and parse failures (they are logged only when verbose), so the extractors' error returns are always nil. extractSkillActivationsFromAgentOutput still needs those errors, so it cannot reuse this helper as written. Consider having the helper return an error (or accept an error-returning callback) so the last copy of the loop can be removed.

@copilot please address this.

Comment thread pkg/cli/logs_metrics.go
})
if verbose {
fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("No safe output artifact found at %s for run %d", agentOutputJSONPath, run.DatabaseID)))
fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Found noop entry: "+item.Message))
@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot

🍳 A little prep to help this PR along! Copilot assignment requested for the following outstanding work:

  1. Update this branch with the latest main using make merge-main, resolving any conflicts and preserving the intended changes.
  2. Fix failing check Analyze (python) (FAILURE): Check run.
  3. Fix failing check impacted-go-tests (FAILURE): Check run.

Keeping the changes focused; human review and CI get the final taste test. No merging or CI triggering.

Structured data
{
  "completed_task": 0,
  "head": "f962496fe5898d2be782fe79b1e8b507db48fe96",
  "kind": "requested",
  "schema_version": "1",
  "state": "9de73fc68db83d14741dbef8fc0604256ec967a5fcf563350182608e20ab1100",
  "work": [
    "42e5d572d9d75b7168fe7d0aeda0717cc07e65ed240f6d299e6fec661214f1ac",
    "43282e7f5b3023b8c8cf9099eab79e7b99f650965f08b47d3df33cde5589879d"
  ],
  "workflow": "pr-sous-chef"
}

Generated by 👨🍳 PR Sous Chef · pi · routed: haiku55 xhigh · ~2 AIC · ⌖ ~2 AIC · ⊞ 6.9K · ◷
Comment /souschef to run again

Copilot AI and others added 2 commits November 12, 2026 00:19
…output-item-extraction-81256e46f849c011

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>

Copilot AI commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

@copilot

🍳 A little prep to help this PR along! Copilot assignment requested for the following outstanding work:...

Merged main in d0feb5db and isolated the timed-out Go test from the live rate-limit API in 76f678f6; targeted tests pass. Python CodeQL failed because the installation API rate limit was exceeded, so it remains unresolved without a CI rerun. The final validation gate also reports existing logs_metrics.go custom-linter findings; its sampled Go tests passed.

Copilot AI requested a review from gh-aw-bot November 12, 2026 00:32
@pelikhan
pelikhan merged commit 842af2c into main Nov 12, 2026
48 checks passed
@pelikhan
pelikhan deleted the refactor/dedupe-safe-output-item-extraction-81256e46f849c011 branch November 12, 2026 01:33
@github-actions

Copy link
Copy Markdown
Contributor Author

🎉 This pull request is included in a new release.

Release: v1.2.3

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants