Skip to content

refactor: extract checkBoolFailure helper in parseLabelAgentResponse#2106

Merged
lpcox merged 2 commits intomainfrom
copilot/duplicate-code-fix-wasm-response
Mar 18, 2026
Merged

refactor: extract checkBoolFailure helper in parseLabelAgentResponse#2106
lpcox merged 2 commits intomainfrom
copilot/duplicate-code-fix-wasm-response

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 18, 2026

parseLabelAgentResponse in internal/guard/wasm.go contained two byte-for-byte identical 7-line blocks handling "success" and "ok" boolean failure fields — any future change to error formatting or logging had to be applied twice.

Changes

  • New helper checkBoolFailure — extracts the shared failure-detection logic into a single function parameterized by field name:
    func checkBoolFailure(raw map[string]interface{}, resultJSON []byte, key string) error {
        val, ok := raw[key].(bool)
        if !ok || val {
            return nil // field absent or true — not a failure
        }
        if message, msgOK := raw["error"].(string); msgOK && strings.TrimSpace(message) != "" {
            logWasm.Printf("label_agent response indicated failure: error=%s, response=%s", message, string(resultJSON))
            return fmt.Errorf("label_agent rejected policy: %s", message)
        }
        logWasm.Printf("label_agent response indicated non-success status: response=%s", string(resultJSON))
        return fmt.Errorf("label_agent returned non-success status")
    }
  • parseLabelAgentResponse simplified — the two duplicate blocks replaced with:
    if err := checkBoolFailure(raw, resultJSON, "success"); err != nil {
        return nil, err
    }
    if err := checkBoolFailure(raw, resultJSON, "ok"); err != nil {
        return nil, err
    }

Pure refactor; no behavioral change.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build2594320058/b332/launcher.test /tmp/go-build2594320058/b332/launcher.test -test.testlogfile=/tmp/go-build2594320058/b332/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build2594320058/b237/vet.cfg u/13/liblto_plugin.so _64-linux-gnu/13/lto-wrapper x_amd64/asm (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build2594320058/b317/config.test /tmp/go-build2594320058/b317/config.test -test.testlogfile=/tmp/go-build2594320058/b317/testlog.txt -test.paniconexit0 -test.timeout=10m0s conf�� p/go-build mon/httpcommon.go x_amd64/compile (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build2594320058/b332/launcher.test /tmp/go-build2594320058/b332/launcher.test -test.testlogfile=/tmp/go-build2594320058/b332/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build2594320058/b237/vet.cfg u/13/liblto_plugin.so _64-linux-gnu/13/lto-wrapper x_amd64/asm (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build2594320058/b332/launcher.test /tmp/go-build2594320058/b332/launcher.test -test.testlogfile=/tmp/go-build2594320058/b332/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build2594320058/b237/vet.cfg u/13/liblto_plugin.so _64-linux-gnu/13/lto-wrapper x_amd64/asm (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build2594320058/b341/mcp.test /tmp/go-build2594320058/b341/mcp.test -test.testlogfile=/tmp/go-build2594320058/b341/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build2594320058/b251/vet.cfg g_.a ddBm/I3yvPfrQT7ES2YQJddBm x_amd64/link (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

… in parseLabelAgentResponse

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] [duplicate-code] Fix identical failure-check blocks in parseLabelAgentResponse refactor: extract checkBoolFailure helper in parseLabelAgentResponse Mar 18, 2026
Copilot AI requested a review from lpcox March 18, 2026 23:08
@lpcox lpcox marked this pull request as ready for review March 18, 2026 23:24
Copilot AI review requested due to automatic review settings March 18, 2026 23:24
@lpcox lpcox merged commit 76b7179 into main Mar 18, 2026
12 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix-wasm-response branch March 18, 2026 23:24
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 parseLabelAgentResponse in the WASM guard to eliminate duplicated boolean failure-handling logic when interpreting label_agent responses, improving maintainability while preserving behavior.

Changes:

  • Extracted shared "success"/"ok" failure-detection logic into a new checkBoolFailure helper.
  • Simplified parseLabelAgentResponse to delegate those checks to the helper.

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

You can also share your feedback on Copilot code review. Take the survey.

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: Identical WASM failure-check blocks in parseLabelAgentResponse

3 participants