Skip to content

[Repo Assist] refactor(guard): extract decodeWasmCallResult and unmarshalWasmResponse helpers#6397

Merged
lpcox merged 3 commits into
mainfrom
repo-assist/improve-extract-wasm-call-result-19f54fc9b420d565
May 24, 2026
Merged

[Repo Assist] refactor(guard): extract decodeWasmCallResult and unmarshalWasmResponse helpers#6397
lpcox merged 3 commits into
mainfrom
repo-assist/improve-extract-wasm-call-result-19f54fc9b420d565

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Eliminates duplicated WASM call-and-decode logic identified in issues #6384 and #6385.

Changes

internal/guard/wasm_parse.gocloses #6384

tryCallWasmFunction contained two execution paths (allocator path and direct-memory fallback) with ~30 character-for-character identical lines: the fn.Call(...) invocation and the entire buffer-protocol result decode. Both paths now delegate to a new package-level helper:

func decodeWasmCallResult(ctx, fn, mem, inputPtr, inputSize, outputPtr, outputSize) ([]byte, uint32, error)

Any future change to the buffer-too-small protocol (-2 return code), error code interpretation, or output-copy strategy now has a single edit point.

internal/guard/wasm.gocloses #6385

LabelResource and LabelResponse both contained the same json.Unmarshal + identical error message "failed to unmarshal WASM response". Both now call a new helper:

func unmarshalWasmResponse(funcName string, data []byte) (map[string]any, error)

The helper includes the function name in the error message, making it possible to distinguish which call failed in log output.

Test Status

⚠️ Infrastructure limitation: The Go 1.25.0 toolchain required by go.mod cannot be downloaded in this sandbox environment (network access to proxy.golang.org is blocked). Build and tests could not be run locally.

The changes are purely structural refactors with no behavioral changes:

  • All logic is moved verbatim into helpers
  • Both paths still call identical operations in the same order
  • Existing tests in internal/guard/wasm_test.go cover tryCallWasmFunction and callWasmFunction with both memory paths; they will exercise decodeWasmCallResult on CI.

Net diff: −49 lines, +35 lines (net −14 lines of duplication removed)

Warning

Firewall blocked 1 domain

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

  • proxy.golang.org

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

network:
  allowed:
    - defaults
    - "proxy.golang.org"

See Network Configuration for more information.

Generated by Repo Assist · ● 3.2M ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@851905c06e905bf362a9f6cc54f912e3df747d55

…se helpers

Eliminates ~30 lines of duplicated WASM call+decode logic in
tryCallWasmFunction (wasm_parse.go) and ~6 lines of duplicated JSON
unmarshal boilerplate in LabelResource/LabelResponse (wasm.go).

- Extract decodeWasmCallResult: shared fn.Call + buffer-protocol decode
  logic used by both the allocator path and the direct-memory fallback.
  Any future change to buffer sizing, error codes, or copy strategy now
  has a single edit point.
- Extract unmarshalWasmResponse: adds the function name to the error
  message so callers can be distinguished in logs.

Addresses duplicate-code analysis reports #6384 and #6385.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review May 24, 2026 13:50
Copilot AI review requested due to automatic review settings May 24, 2026 13:50
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 the WASM guard integration to remove duplicated “call + decode buffer-protocol” logic and duplicated JSON-unmarshal boilerplate, centralizing both behaviors into shared helpers.

Changes:

  • Extracted decodeWasmCallResult helper to consolidate WASM function invocation + buffer-protocol result decoding in tryCallWasmFunction.
  • Extracted unmarshalWasmResponse helper to centralize JSON decoding and include the WASM function name in unmarshal error messages.
  • Updated LabelResource / LabelResponse to use the new unmarshal helper.
Show a summary per file
File Description
internal/guard/wasm_parse.go Removes duplicated WASM call/result decoding by delegating both memory paths to decodeWasmCallResult.
internal/guard/wasm.go Uses unmarshalWasmResponse to share JSON decoding and improve error context for labeling calls.

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: 3

@@ -296,21 +276,17 @@ func (g *WasmGuard) tryCallWasmFunction(ctx context.Context, fn api.Function, me
return nil, 0, fmt.Errorf("WASM function call failed: %w", err)
}
Comment thread internal/guard/wasm.go Outdated
Comment on lines +522 to +530
return nil, difc.OperationWrite, fmt.Errorf("failed to unmarshal WASM response: %w", err)
response, err := unmarshalWasmResponse("label_resource", resultJSON)
if err != nil {
return nil, difc.OperationWrite, err
Comment thread internal/guard/wasm.go Outdated
Comment on lines +556 to +569
return nil, fmt.Errorf("failed to unmarshal WASM response: %w", err)
responseMap, err := unmarshalWasmResponse("label_response", resultJSON)
if err != nil {
return nil, err
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 24, 2026

@copilot address review feedback

Copy link
Copy Markdown
Contributor

Copilot AI commented May 24, 2026

@copilot address review feedback

Addressed in commits c1b1985 and 3d91a22.

  • Added a safe empty-result check in decodeWasmCallResult and covered it with a regression test.
  • Hardened memory handling to avoid panics from invalid/typed-nil WASM memory interfaces.
  • Deduplicated function-name literals in LabelResource/LabelResponse via local funcName constants.
  • Added helper docs requested by validation review.

Copilot finished work on behalf of lpcox May 24, 2026 23:08
Copilot AI requested a review from lpcox May 24, 2026 23:08
@lpcox lpcox merged commit 0eed00d into main May 24, 2026
16 checks passed
@lpcox lpcox deleted the repo-assist/improve-extract-wasm-call-result-19f54fc9b420d565 branch May 24, 2026 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants