Skip to content

Duplicate code: input-schema generation logic repeated across workflow dispatch tool builders #25794

@github-actions

Description

@github-actions

Overview

Semantic analysis found a repeated input-schema construction block across three Go functions that generate MCP tool definitions. The duplication is substantial (>60 lines each), and a behavior change in one function is likely to drift from the others.

Critical Findings

  • Pattern: JSON schema property/required generation from workflow/tool input definitions
  • Severity: Medium
  • Occurrences: 3
  • Threshold check: Exceeds both criteria (>10 duplicated lines and 3+ similar instances)

Duplication Locations

  • pkg/workflow/safe_outputs_call_workflow.go (generateCallWorkflowTool, lines 88-147)
  • pkg/workflow/safe_outputs_dispatch.go (generateDispatchWorkflowTool, lines 121-180)
  • pkg/workflow/dispatch_repository.go (generateDispatchRepositoryTool, lines 151-208)
Representative duplicated block (logic shape)
for inputName, inputDef := range workflowInputs {
    inputDefMap, ok := inputDef.(map[string]any)
    if !ok {
        continue
    }

    inputType := "string"
    inputDescription := ...
    inputRequired := false

    if desc, ok := inputDefMap["description"].(string); ok && desc != "" {
        inputDescription = desc
    }
    if req, ok := inputDefMap["required"].(bool); ok {
        inputRequired = req
    }

    if typeStr, ok := inputDefMap["type"].(string); ok {
        switch typeStr {
        case "number":
            inputType = "number"
        case "boolean":
            inputType = "boolean"
        case "choice":
            inputType = "string"
            if options, ok := inputDefMap["options"].([]any); ok && len(options) > 0 {
                // enum + required handling
                continue
            }
        case "environment":
            inputType = "string"
        }
    }

    // default + properties + required handling
}

Impact

  • Maintainability: Bug fixes or schema updates require touching 3 sites with subtle differences.
  • Behavior drift risk: Type mapping/default/required handling can diverge.
  • Code bloat: Repeated branching and map wiring obscures the core intent of each generator.

Refactoring Recommendation

  1. Extract shared helper in pkg/workflow/ (e.g., build_input_schema.go):
  • Input: map[string]any definitions + description callback/options.
  • Output: properties map[string]any, required []string.
  • Preserve current special cases (choice enum/default handling).
  1. Keep generator-specific metadata local:
  • _workflow_name, _call_workflow_name, _dispatch_repository_tool, and description text should remain in their owning function.
  1. Add targeted tests for helper once:
  • Type mapping for string|number|boolean|choice|environment.
  • Required sorting behavior and default propagation.
  • Invalid input-def shape (!map[string]any) skip behavior.

Implementation Checklist

  • Extract shared schema builder helper
  • Replace duplicated loops in 3 generators
  • Add/adjust tests for type/default/required behavior
  • Verify generated tool schemas are unchanged (golden/assertion tests)

Analysis Metadata

  • Analyzed files: 932 eligible .go/.cjs files (excluding tests and workflows)
  • Method: Serena semantic analysis + structural duplicate scan
  • Commit: 87085b3477d30beea73e9e5668ff9a9b3224a6d5
  • Workflow run: §24281836678
  • Triggered by: @pelikhan

Generated by Duplicate Code Detector ·

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions