Skip to content

Deduplicate declarative constraint builders in tool_description_enhancer.go - #52157

Merged
pelikhan merged 5 commits into
mainfrom
copilot/duplicate-code-tool-description-enhancer
Aug 12, 2026
Merged

Deduplicate declarative constraint builders in tool_description_enhancer.go#52157
pelikhan merged 5 commits into
mainfrom
copilot/duplicate-code-tool-description-enhancer

Conversation

Copilot AI commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

tool_description_enhancer.go had ~33 per-tool constraint builder functions, each repeating the same skeleton: nil guard, slice setup, a handful of field gates, and a return. The control flow was duplicated across the whole file, with only strings and field selectors differing between builders.

Shared helpers

  • Added a generic buildConstraints[T any](config *T, build func(*T, *[]string)) []string that centralizes the nil-check + slice-setup boilerplate previously repeated in every builder.
  • Added appendTargetConstraint, appendTargetRepoSlugConstraint, and appendRequiredTitlePrefixConstraint for the three most common field-gate patterns (appeared 12, 13, and 5 times respectively).

Builder refactor

  • Rewrote every *Constraints function (createIssueConstraints, closeDiscussionConstraints, updatePullRequestConstraints, etc.) to use the new helpers instead of hand-rolled nil checks and repeated fmt.Sprintf calls for common fields.
  • Preserved all declarative differences (message strings, field selectors, special cases like addCommentConstraints always appending a trailing constraint) exactly as before — no observable behavior change.

Example of the pattern change:

// Before
func closeIssueConstraints(config *CloseIssuesConfig) []string {
	if config == nil {
		return nil
	}
	var constraints []string
	appendMaxConstraint(&constraints, config.Max, "Maximum %d issue(s) can be closed.")
	if config.Target != "" {
		constraints = append(constraints, fmt.Sprintf("Target: %s.", config.Target))
	}
	...
	return constraints
}

// After
func closeIssueConstraints(config *CloseIssuesConfig) []string {
	return buildConstraints(config, func(config *CloseIssuesConfig, constraints *[]string) {
		appendMaxConstraint(constraints, config.Max, "Maximum %d issue(s) can be closed.")
		appendTargetConstraint(constraints, config.Target)
		...
	})
}

Net effect: the file shrinks from 740 to 585 lines while the per-tool declarative logic remains fully readable and easy to extend for new tools.


Generated by 👨‍🍳 PR Sous Chef · gpt54 · 15.2 AIC · ⌖ 5.24 AIC · ⊞ 8.5K ·
Comment /souschef to run again

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate constraint builders in tool description enhancer Deduplicate declarative constraint builders in tool_description_enhancer.go Aug 11, 2026
Copilot AI requested a review from pelikhan August 11, 2026 22:24
@pelikhan
pelikhan marked this pull request as ready for review August 11, 2026 22:39
Copilot AI balanced review requested due to automatic review settings August 11, 2026 22:39
@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

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

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

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Test Quality Sentinel completed test quality analysis.

No test files were added or modified in this PR. Test Quality Sentinel skipped.

🧪 Test quality analysis by Test Quality Sentinel

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

PR Code Quality Reviewer completed the code quality review.

Warning

Threat Detection Engine Failure — The analysis engine could not complete. This is a tooling failure, not a security finding.

What happened

The threat detection engine failed to produce results.

Review the workflow run logs for details.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • api.individual.githubcopilot.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.individual.githubcopilot.com"

See Network Configuration for more information.

🔎 Code quality review by PR Code Quality Reviewer

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Ponytail Reviewer completed successfully!

Lean already. Ship. Reviewed the refactor of tool_description_enhancer.go: this is genuine deduplication, not over-engineering. buildConstraints[T] and the three field-gate helpers (appendTargetConstraint, appendTargetRepoSlugConstraint, appendRequiredTitlePrefixConstraint) each collapse 5-13 duplicated call sites, net -155 lines, no new speculative abstractions or unused flexibility introduced. Nothing to cut.

Generated by Ponytail Reviewer for #52157

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Design Decision Gate 🏗️ failed during design decision gate check.

🏗️ ADR gate enforced by Design Decision Gate 🏗️

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.

Pull request overview

Refactors tool-description constraint builders to remove duplicated control flow while preserving behavior.

Changes:

  • Adds a generic constraint-building helper.
  • Extracts common target, repository, and title-prefix constraints.
  • Migrates all per-tool builders to shared helpers.
Show a summary per file
File Description
pkg/workflow/tool_description_enhancer.go Deduplicates constraint-builder boilerplate and common field gates.

Review details

Tip

Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Balanced

@github-actions github-actions Bot 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.

Skills-Based Review 🧠

Applied /codebase-design and /tdd — commenting with two design refinements and one test coverage gap.

📋 Key Themes & Highlights

Key Themes

  • Duplicate helpers: appendTargetRepoSlugConstraint and appendRequiredTitlePrefixConstraint are byte-for-byte identical; they should be one appendStringConstraint.
  • Incomplete helper adoption: linkSubIssueConstraints still uses raw *constraints = append(...) for ParentTitlePrefix/SubTitlePrefix instead of the new helper.
  • Missing regression test: the addCommentConstraints nil-config special case (trailing constraint always appended) has no test coverage.

Positive Highlights

  • buildConstraints[T] is a clean, generic nil-guard that eliminates ~33 boilerplate blocks — the core abstraction is well-designed.
  • ✅ PR description is clear and the before/after example makes the pattern immediately legible.
  • ✅ All declarative differences are preserved exactly; the refactor is structurally sound.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · sonnet46 · 41.6 AIC · ⌖ 7.22 AIC · ⊞ 7K
Comment /matt to run again

if prefix != "" {
*constraints = append(*constraints, fmt.Sprintf(format, prefix))
}
}

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.

[/codebase-design] appendTargetRepoSlugConstraint and appendRequiredTitlePrefixConstraint have identical implementations — both guard on a non-empty string and call fmt.Sprintf(format, value). The semantic distinction lives in the caller's format string, not the helper itself, so two functions add naming complexity without adding behaviour.

💡 Suggested consolidation

Collapse both (and potentially appendTargetConstraint) into one helper:

// appendStringConstraint appends a formatted constraint when value is non-empty.
func appendStringConstraint(constraints *[]string, value, format string) {
    if value != "" {
        *constraints = append(*constraints, fmt.Sprintf(format, value))
    }
}

All call sites stay self-descriptive via their format argument. appendTargetConstraint can then become a thin wrapper or be inlined.

@copilot please address this.

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.

Consolidated in 1e5bd53. appendTargetRepoSlugConstraint and appendRequiredTitlePrefixConstraint are replaced by a single appendStringConstraint(constraints *[]string, value, format string); appendTargetConstraint is now a thin wrapper over it for the common "Target: %s." message. Every non-empty-string field gate in the file (18 sites) now routes through it, so the file is down to 550 lines.

*constraints = append(*constraints, fmt.Sprintf("The parent issue title must start with %q.", config.ParentTitlePrefix))
}
if config.SubTitlePrefix != "" {
*constraints = append(*constraints, fmt.Sprintf("The sub-issue title must start with %q.", config.SubTitlePrefix))

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.

[/codebase-design] linkSubIssueConstraints uses raw *constraints = append(...) for ParentTitlePrefix and SubTitlePrefix instead of the new appendRequiredTitlePrefixConstraint helper. This inconsistency means a future reader has to track two styles for the same pattern.

💡 Suggested fix
appendRequiredTitlePrefixConstraint(constraints, config.ParentTitlePrefix, "The parent issue title must start with %q.")
appendRequiredTitlePrefixConstraint(constraints, config.SubTitlePrefix, "The sub-issue title must start with %q.")

@copilot please address this.

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.

Fixed in 1e5bd53. linkSubIssueConstraints now uses the shared helper (appendStringConstraint) for both ParentTitlePrefix and SubTitlePrefix, and the same conversion was applied to every other remaining hand-rolled non-empty-string gate in the file so there is a single style throughout.

if config.TargetRepoSlug != "" {
constraints = append(constraints, fmt.Sprintf("Comments will be added in repository %q.", config.TargetRepoSlug))
}
constraints := buildConstraints(config, func(config *AddCommentsConfig, constraints *[]string) {

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.

[/tdd] addCommentConstraints is the only builder that has a non-nil-guard code path (it always appends a trailing constraint even when config == nil). The refactor preserves that behaviour, but there are no tests asserting it — a future cleanup could accidentally drop the trailing "Supports reply_to_id for discussion threading." line.

💡 Suggested test
func TestAddCommentConstraintsNilConfig(t *testing.T) {
    got := addCommentConstraints(nil)
    require.Equal(t, []string{"Supports reply_to_id for discussion threading."}, got)
}

This makes the special-case contract explicit and guards against regression.

@copilot please address this.

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.

Added TestAddCommentConstraintsNilConfig in 1e5bd53 (pkg/workflow/tool_description_enhancer_test.go), asserting addCommentConstraints(nil) returns exactly ["Supports reply_to_id for discussion threading."]. ADR-52157 also notes the test pins this contract.

@github-actions github-actions Bot 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.

🧵 Reviewed using Impeccable skills by Impeccable Skills Reviewer · sonnet46 · 40.2 AIC · ⌖ 6.21 AIC · ⊞ 5.4K

return constraints
}

// appendTargetConstraint appends the common "Target: <value>." constraint when target is set.

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.

Minor: appendTargetRepoSlugConstraint and appendRequiredTitlePrefixConstraint are functionally identical. Both guard on a non-empty string and call fmt.Sprintf(format, value). Consider consolidating into a single appendStringConstraint(constraints *[]string, value, format string) helper to avoid duplication of the same one-liner. The distinct names are readable, but if more single-string-guarded helpers are added in the future they would also each need their own function. This is a minor suggestion, not a blocker.

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.

Done in 1e5bd53 — both were collapsed into a single appendStringConstraint(constraints *[]string, value, format string) helper, with appendTargetConstraint kept as a thin wrapper. All other single-string-guarded appends in the file now use it too, so future additions won't need new per-field helpers.

@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot This PR looks ready for a finishing pass. Please refresh the branch context, address the latest reviewer feedback, update the branch state if needed, and run the pr-finisher skill before handing back to maintainers.

Open items:

  • Review the latest skills-based feedback from github-actions and either apply the requested refinements or explain why they are not needed.
  • The Design Decision Gate run reported a failure; inspect the linked run and clear any remaining gate requirements.
  • After that, re-check CI status and summarize what changed in your reply.

Run context: https://github.com/github/gh-aw/actions/runs/31545290203

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 15.2 AIC · ⌖ 5.24 AIC · ⊞ 8.5K ·
Comment /souschef to run again

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
Copilot AI requested a review from gh-aw-bot August 11, 2026 23:27
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Triage: PR #52157

Generated by 🔧 PR Triage Agent · auto · 44.5 AIC · ⌖ 2.66 AIC · ⊞ 7.8K ·

@pelikhan
pelikhan merged commit 360bc2e into main Aug 12, 2026
29 checks passed
@pelikhan
pelikhan deleted the copilot/duplicate-code-tool-description-enhancer branch August 12, 2026 01:10
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: Declarative constraint builders repeated in tool_description_enhancer

4 participants