Skip to content

Commit 3bdad45

Browse files
authored
Merge pull request #1895 from dgageot/fixes_daily
Daily fixes of the bot-detected issues
2 parents a3526f4 + 2b69353 commit 3bdad45

7 files changed

Lines changed: 97 additions & 16 deletions

File tree

cmd/root/debug_auth.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ func parseAuthInfo(token string) (*authInfo, error) {
102102
}
103103

104104
func printAuthInfoText(w io.Writer, info *authInfo) {
105-
fmt.Fprintf(w, "Token: %s...%s\n", info.Token[:10], info.Token[len(info.Token)-10:])
105+
const previewLen = 10
106+
if len(info.Token) <= previewLen*2 {
107+
fmt.Fprintf(w, "Token: %s\n", info.Token)
108+
} else {
109+
fmt.Fprintf(w, "Token: %s...%s\n", info.Token[:previewLen], info.Token[len(info.Token)-previewLen:])
110+
}
106111

107112
if info.Username != "" {
108113
fmt.Fprintf(w, "Username: %s\n", info.Username)

cmd/root/otel.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package root
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"os"
8+
"strings"
79
"time"
810

911
"go.opentelemetry.io/otel"
@@ -34,10 +36,13 @@ func initOTelSDK(ctx context.Context) (err error) {
3436

3537
// Only initialize if endpoint is configured
3638
if endpoint != "" {
37-
traceExporter, err = otlptracehttp.New(ctx,
39+
opts := []otlptracehttp.Option{
3840
otlptracehttp.WithEndpoint(endpoint),
39-
otlptracehttp.WithInsecure(), // TODO: make configurable
40-
)
41+
}
42+
if isLocalhostEndpoint(endpoint) {
43+
opts = append(opts, otlptracehttp.WithInsecure())
44+
}
45+
traceExporter, err = otlptracehttp.New(ctx, opts...)
4146
if err != nil {
4247
return fmt.Errorf("failed to create trace exporter: %w", err)
4348
}
@@ -67,3 +72,17 @@ func initOTelSDK(ctx context.Context) (err error) {
6772

6873
return nil
6974
}
75+
76+
// isLocalhostEndpoint reports whether the given endpoint refers to a
77+
// loopback address so that we can safely skip TLS.
78+
func isLocalhostEndpoint(endpoint string) bool {
79+
host := endpoint
80+
// Strip port if present.
81+
if h, _, err := net.SplitHostPort(endpoint); err == nil {
82+
host = h
83+
}
84+
// Strip brackets from IPv6 addresses (e.g. "[::1]" without a port).
85+
host = strings.TrimPrefix(host, "[")
86+
host = strings.TrimSuffix(host, "]")
87+
return host == "localhost" || host == "127.0.0.1" || host == "::1"
88+
}

cmd/root/otel_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package root
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsLocalhostEndpoint(t *testing.T) {
10+
t.Parallel()
11+
12+
tests := []struct {
13+
name string
14+
endpoint string
15+
want bool
16+
}{
17+
{"localhost no port", "localhost", true},
18+
{"localhost with port", "localhost:4318", true},
19+
{"ipv4 loopback no port", "127.0.0.1", true},
20+
{"ipv4 loopback with port", "127.0.0.1:4318", true},
21+
{"ipv6 loopback no port", "::1", true},
22+
{"ipv6 loopback bracketed", "[::1]", true},
23+
{"ipv6 loopback with port", "[::1]:4318", true},
24+
{"remote host", "example.com", false},
25+
{"remote host with port", "example.com:4318", false},
26+
{"remote ip", "192.168.1.1", false},
27+
{"remote ip with port", "192.168.1.1:4318", false},
28+
{"empty string", "", false},
29+
}
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
t.Parallel()
33+
assert.Equal(t, tt.want, isLocalhostEndpoint(tt.endpoint))
34+
})
35+
}
36+
}

pkg/connectrpc/server.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,22 @@ func toolCallToProto(tc tools.ToolCall) *cagentv1.ToolCall {
361361
func toolToProto(t tools.Tool) *cagentv1.Tool {
362362
var paramsJSON []byte
363363
if t.Parameters != nil {
364-
paramsJSON, _ = json.Marshal(t.Parameters)
364+
var err error
365+
paramsJSON, err = json.Marshal(t.Parameters)
366+
if err != nil {
367+
slog.Warn("Failed to marshal tool parameters", "tool", t.Name, "error", err)
368+
paramsJSON = nil
369+
}
365370
}
366371

367372
var outputSchemaJSON []byte
368373
if t.OutputSchema != nil {
369-
outputSchemaJSON, _ = json.Marshal(t.OutputSchema)
374+
var err error
375+
outputSchemaJSON, err = json.Marshal(t.OutputSchema)
376+
if err != nil {
377+
slog.Warn("Failed to marshal tool output schema", "tool", t.Name, "error", err)
378+
outputSchemaJSON = nil
379+
}
370380
}
371381

372382
annotations := &cagentv1.ToolAnnotations{

pkg/runtime/runtime.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ func (r *LocalRuntime) registerDefaultTools() {
936936
}
937937

938938
func (r *LocalRuntime) finalizeEventChannel(ctx context.Context, sess *session.Session, events chan Event) {
939+
// Clear the elicitation events channel before closing the events channel
940+
// to prevent a send-on-closed-channel panic in elicitationHandler.
941+
r.clearElicitationEventsChannel()
942+
939943
defer close(events)
940944

941945
events <- StreamStopped(sess.ID, r.currentAgent)
@@ -961,7 +965,6 @@ func (r *LocalRuntime) RunStream(ctx context.Context, sess *session.Session) <-c
961965

962966
// Set the events channel for elicitation requests
963967
r.setElicitationEventsChannel(events)
964-
defer r.clearElicitationEventsChannel()
965968

966969
// Set elicitation handler on all MCP toolsets before getting tools
967970
a := r.CurrentAgent()
@@ -2095,12 +2098,12 @@ func (r *LocalRuntime) clearElicitationEventsChannel() {
20952098
func (r *LocalRuntime) elicitationHandler(ctx context.Context, req *mcp.ElicitParams) (tools.ElicitationResult, error) {
20962099
slog.Debug("Elicitation request received from MCP server", "message", req.Message)
20972100

2098-
// Get the current events channel
2101+
// Hold the read lock while sending to the channel to prevent a race
2102+
// with clearElicitationEventsChannel / close(events).
20992103
r.elicitationEventsChannelMux.RLock()
21002104
eventsChannel := r.elicitationEventsChannel
2101-
r.elicitationEventsChannelMux.RUnlock()
2102-
21032105
if eventsChannel == nil {
2106+
r.elicitationEventsChannelMux.RUnlock()
21042107
return tools.ElicitationResult{}, fmt.Errorf("no events channel available for elicitation")
21052108
}
21062109

@@ -2111,6 +2114,7 @@ func (r *LocalRuntime) elicitationHandler(ctx context.Context, req *mcp.ElicitPa
21112114

21122115
// Send elicitation request event to the runtime's client
21132116
eventsChannel <- ElicitationRequest(req.Message, req.Mode, req.RequestedSchema, req.URL, req.ElicitationID, req.Meta, r.currentAgent)
2117+
r.elicitationEventsChannelMux.RUnlock()
21142118

21152119
// Wait for response from the client
21162120
select {

pkg/server/session_manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ func (sm *SessionManager) GetAgentToolCount(ctx context.Context, agentFilename,
402402
if err != nil {
403403
return 0, err
404404
}
405+
defer func() {
406+
if stopErr := t.StopToolSets(ctx); stopErr != nil {
407+
slog.Error("Failed to stop tool sets", "error", stopErr)
408+
}
409+
}()
405410

406411
a, err := t.Agent(agentName)
407412
if err != nil {

pkg/tools/builtin/lsp.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,16 +1456,18 @@ func (h *lspHandler) readMessageLocked() ([]byte, error) {
14561456
}
14571457

14581458
func (h *lspHandler) readNotifications(ctx context.Context, stderrBuf *bytes.Buffer) {
1459+
ticker := time.NewTicker(100 * time.Millisecond)
1460+
defer ticker.Stop()
1461+
14591462
for {
14601463
select {
14611464
case <-ctx.Done():
14621465
return
1463-
default:
1464-
}
1465-
1466-
if stderrBuf.Len() > 0 {
1467-
slog.Debug("LSP stderr", "content", stderrBuf.String())
1468-
stderrBuf.Reset()
1466+
case <-ticker.C:
1467+
if stderrBuf.Len() > 0 {
1468+
slog.Debug("LSP stderr", "content", stderrBuf.String())
1469+
stderrBuf.Reset()
1470+
}
14691471
}
14701472
}
14711473
}

0 commit comments

Comments
 (0)