Skip to content

[fp-enhancer] fp-enhancer: Improve pkg/agentdrain (round 1/20)#26177

Merged
pelikhan merged 1 commit intomainfrom
fp-enhancer/pkg-agentdrain-4195f9659e4f7f19
Apr 14, 2026
Merged

[fp-enhancer] fp-enhancer: Improve pkg/agentdrain (round 1/20)#26177
pelikhan merged 1 commit intomainfrom
fp-enhancer/pkg-agentdrain-4195f9659e4f7f19

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Apply moderate functional/immutability improvements to pkg/agentdrain — the Drain log template miner package.

Summary

3 files changed, 17 insertions(+), 19 deletions(−)

Improvement Files Benefit
slices.Clone for defensive copies cluster.go, persist.go Clearer intent, less boilerplate
Pre-allocate snap.Clusters slice persist.go Functional initialization, avoids silent grow
sliceutil.FilterMapKeys in FlattenEvent mask.go Declarative filter, uses existing helper

Changes

pkg/agentdrain/cluster.go

  • add(): replace make([]string, len)+copy with slices.Clone(template) — expresses defensive copy intent directly

pkg/agentdrain/persist.go

  • SaveJSON(): pre-allocate Clusters field in the Snapshot struct literal with make([]SnapshotCluster, 0, len(m.store.clusters)) to avoid silent re-allocation
  • SaveJSON() + LoadJSON(): replace make+copy with slices.Clone (2 occurrences)

pkg/agentdrain/mask.go

  • FlattenEvent(): replace imperative filter loop with sliceutil.FilterMapKeys — the helper already exists in the codebase and the map predicate reads naturally

Behavior

No behavior changes — all transformations are semantically equivalent.

Testing

  • ✅ All pkg/agentdrain tests pass (go test ./pkg/agentdrain/)
  • go vet clean
  • go fmt applied

Round-Robin Progress

Processed: pkg/agentdrain (1 of 20 packages)
Next run: pkg/cli

References:

Generated by Functional Pragmatist · ● 2.1M ·

  • expires on Apr 15, 2026, 10:03 AM UTC

- Use slices.Clone for defensive template copying in cluster.go,
  persist.go (replaces verbose make+copy pattern in 3 places)
- Pre-allocate snap.Clusters slice with known capacity in SaveJSON
- Use sliceutil.FilterMapKeys in FlattenEvent to replace imperative
  filter loop with declarative expression

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan pelikhan marked this pull request as ready for review April 14, 2026 11:27
Copilot AI review requested due to automatic review settings April 14, 2026 11:27
@pelikhan pelikhan merged commit f809715 into main Apr 14, 2026
53 checks passed
@pelikhan pelikhan deleted the fp-enhancer/pkg-agentdrain-4195f9659e4f7f19 branch April 14, 2026 11:27
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

This PR applies small functional/immutability refactors in pkg/agentdrain (Drain log template miner), primarily around defensive slice copying and a more declarative map-key filtering approach.

Changes:

  • Replace manual make+copy defensive copies with slices.Clone in cluster creation and persistence.
  • Pre-allocate the Snapshot.Clusters slice in SaveJSON.
  • Simplify FlattenEvent key filtering using sliceutil.FilterMapKeys.
Show a summary per file
File Description
pkg/agentdrain/persist.go Uses slices.Clone for template copies and pre-allocates Snapshot.Clusters during JSON save/load.
pkg/agentdrain/mask.go Refactors event field key collection via sliceutil.FilterMapKeys before sorting.
pkg/agentdrain/cluster.go Uses slices.Clone for defensive template copying when creating new clusters.

Copilot's findings

Tip

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

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Comment on lines +35 to 38
Config: m.cfg,
NextID: m.store.nextID,
Clusters: make([]SnapshotCluster, 0, len(m.store.clusters)),
}
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

SaveJSON now initializes Snapshot.Clusters with an empty (non-nil) slice. When there are zero clusters this changes the serialized JSON from "clusters": null (previous behavior) to "clusters": [], which is an observable wire-format change (and may diverge from existing persisted snapshots / pkg/agentdrain/data/default_weights.json that currently contains clusters: null for some stages). If compatibility is important, keep Clusters nil when len(m.store.clusters)==0 (only preallocate when non-empty), or explicitly decide on the new format and update the PR description + any committed snapshot data accordingly.

Suggested change
Config: m.cfg,
NextID: m.store.nextID,
Clusters: make([]SnapshotCluster, 0, len(m.store.clusters)),
}
Config: m.cfg,
NextID: m.store.nextID,
}
if len(m.store.clusters) > 0 {
snap.Clusters = make([]SnapshotCluster, 0, len(m.store.clusters))
}

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot mentioned this pull request Apr 14, 2026
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.

2 participants