-
Notifications
You must be signed in to change notification settings - Fork 21
fix: eliminate three duplicate-code patterns across server and guard packages #2852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -600,6 +600,26 @@ func parseLabelAgentResponse(resultJSON []byte) (*LabelAgentResult, error) { | |||||||
| return &result, nil | ||||||||
| } | ||||||||
|
|
||||||||
| // callWasmGuardFunction serialises WASM access, sets the backend reference, marshals | ||||||||
| // inputData, logs the input, calls the named WASM export, and returns the raw result. | ||||||||
| // All three public dispatch methods (LabelAgent, LabelResource, LabelResponse) share | ||||||||
| // this preamble; keeping it in one place ensures locking and backend-update logic | ||||||||
| // cannot drift between them. | ||||||||
| func (g *WasmGuard) callWasmGuardFunction(ctx context.Context, funcName string, backend BackendCaller, inputData map[string]interface{}) ([]byte, error) { | ||||||||
| g.mu.Lock() | ||||||||
| defer g.mu.Unlock() | ||||||||
|
|
||||||||
| g.backend = backend | ||||||||
|
|
||||||||
| inputJSON, err := json.Marshal(inputData) | ||||||||
| if err != nil { | ||||||||
| return nil, fmt.Errorf("failed to marshal %s input: %w", funcName, err) | ||||||||
| } | ||||||||
| logWasm.Printf("%s input JSON (%d bytes): %s", funcName, len(inputJSON), string(inputJSON)) | ||||||||
|
||||||||
| logWasm.Printf("%s input JSON (%d bytes): %s", funcName, len(inputJSON), string(inputJSON)) | |
| // Log only the function name and payload size to avoid leaking potentially large or sensitive data. | |
| logWasm.Printf("%s input JSON (%d bytes)", funcName, len(inputJSON)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ func handleClose(unifiedServer *UnifiedServer) http.Handler { | |
| // Only accept POST requests | ||
| if r.Method != http.MethodPost { | ||
| logHandlers.Printf("Close request rejected: invalid method=%s", r.Method) | ||
| http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
| writeErrorResponse(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed") | ||
| return | ||
|
Comment on lines
41
to
45
|
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,16 @@ func writeJSONResponse(w http.ResponseWriter, statusCode int, body interface{}) | |
| httputil.WriteJSONResponse(w, statusCode, body) | ||
| } | ||
|
|
||
| // writeErrorResponse writes a JSON error response with a consistent shape. | ||
| // All HTTP error paths in the server package should use this helper to ensure | ||
| // clients always receive application/json rather than text/plain. | ||
| func writeErrorResponse(w http.ResponseWriter, statusCode int, code, message string) { | ||
| writeJSONResponse(w, statusCode, map[string]string{ | ||
| "error": code, | ||
| "message": message, | ||
| }) | ||
| } | ||
|
Comment on lines
+26
to
+34
|
||
|
|
||
| // withResponseLogging wraps an http.Handler to log response bodies | ||
| func withResponseLogging(handler http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,13 +241,9 @@ func (g *guardBackendCaller) CallTool(ctx context.Context, toolName string, args | |
| // This bypasses DIFC checks since it's internal to the guard | ||
| log.Printf("[DIFC] Guard calling backend %s tool %s for metadata", g.serverID, toolName) | ||
|
|
||
| // Get or launch backend connection (use session-aware connection for stateful backends) | ||
| sessionID := g.ctx.Value(SessionIDContextKey) | ||
| if sessionID == nil { | ||
| sessionID = "default" | ||
| } | ||
| sessionID := SessionIDFromContext(g.ctx) | ||
|
|
||
| return executeBackendToolCall(g.ctx, g.server.launcher, g.serverID, sessionID.(string), toolName, args) | ||
| return executeBackendToolCall(g.ctx, g.server.launcher, g.serverID, sessionID, toolName, args) | ||
|
Comment on lines
242
to
+246
|
||
| } | ||
|
|
||
| // newErrorCallToolResult creates a standard error CallToolResult with the error message | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This refactor moves some work in LabelAgent/LabelResource/LabelResponse outside the WASM mutex, but the surrounding documentation elsewhere in the file still states that public methods hold g.mu for their entire duration. Please update the related comments to reflect the new locking boundary so future changes don’t rely on outdated assumptions.