Skip to content

Refactor duplicated gojq execution path in jqschema middleware#5427

Merged
lpcox merged 3 commits intomainfrom
copilot/fix-duplicate-code-jq-filter-logic
May 10, 2026
Merged

Refactor duplicated gojq execution path in jqschema middleware#5427
lpcox merged 3 commits intomainfrom
copilot/fix-duplicate-code-jq-filter-logic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 10, 2026

✨ Enhancement

applyJqSchema and applyToolResponseFilter each implemented nearly identical gojq execution/timeout/error-handling logic, creating a maintenance hotspot in one file. This change consolidates that shared path into a single helper while preserving behavior-specific differences (notably tool-filter multi-result enforcement).

  • What does this improve?

    • Removes duplicated gojq run/error logic from two middleware functions.
    • Centralizes timeout + gojq.HaltError + context-cancellation handling in one place.
    • Keeps per-callsite semantics intact (jq query execution failed wording for schema path, multi-result rejection for tool response filters).
  • Why is this valuable?

    • Future changes to jq execution behavior now land in one code path instead of two.
    • Reduces risk of drift between schema filtering and tool response filtering error behavior.
  • Implementation approach:

    • Added private helper runJqCode(...) with runJqCodeOptions for controlled per-caller behavior:
      • optional timeout logging
      • optional execution error prefix override
      • optional single-result enforcement
    • Updated:
      • applyJqSchema to delegate to runJqCode with schema-specific execution prefix.
      • applyToolResponseFilter to delegate with CheckMultipleResults: true.
    • Added focused tests for tool-response filter edge contracts:
      • no output (empty) returns tool response filter returned no results
      • multi-output (.[]) returns tool response filter returned multiple results...
v, err := runJqCode(ctx, jqSchemaCode, jsonData, "jq schema filter", runJqCodeOptions{
    ExecutionPrefix:   "jq query",
    LogDefaultTimeout: true,
})

return runJqCode(ctx, code, jsonData, "tool response filter", runJqCodeOptions{
    CheckMultipleResults: true,
})

Copilot AI changed the title [WIP] Refactor duplicate jq filter execution logic in jqschema.go Refactor duplicated gojq execution path in jqschema middleware May 10, 2026
Copilot finished work on behalf of lpcox May 10, 2026 14:10
Copilot AI requested a review from lpcox May 10, 2026 14:10
@lpcox lpcox marked this pull request as ready for review May 10, 2026 15:21
Copilot AI review requested due to automatic review settings May 10, 2026 15:21
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

Refactors internal/middleware/jqschema.go to remove duplicated gojq execution/timeout/error-handling logic by introducing a shared helper, while preserving the distinct error-message prefixes and the tool-response filter’s single-result contract.

Changes:

  • Added runJqCode(...) + runJqCodeOptions to centralize gojq execution, default-timeout application, and gojq.HaltError/context error handling.
  • Updated applyJqSchema and applyToolResponseFilter to delegate to runJqCode, with schema-specific execution prefixing and tool-filter multi-result enforcement.
  • Added targeted tests covering “no results” and “multiple results” edge cases for tool-response filters.
Show a summary per file
File Description
internal/middleware/jqschema.go Consolidates duplicated gojq execution logic into runJqCode while keeping caller-specific semantics (execution prefix + multi-result enforcement).
internal/middleware/jqschema_test.go Adds tests for tool response filter edge contracts (empty output and multi-output).

Copilot's findings

Tip

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

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@lpcox lpcox merged commit a4778fc into main May 10, 2026
30 checks passed
@lpcox lpcox deleted the copilot/fix-duplicate-code-jq-filter-logic branch May 10, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Pattern: jq Filter Execution Logic in jqschema.go

3 participants