From ecedd32c9711f3d480fcb5b8d86ef00b393424af Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:00:00 +0800 Subject: [PATCH] fix(pi): omit empty required lists in tool schemas (#4) Prevent a parameterless TaskList schema from rejecting a plain chat request. Add a regression test through the Pi worker and an HTTP fixture. --- internal/multiagent/pi_integration_test.go | 52 ++++++++++++++++++++++ internal/multiagent/pi_runner.go | 7 ++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/internal/multiagent/pi_integration_test.go b/internal/multiagent/pi_integration_test.go index 8b7dc179..630dbfe9 100644 --- a/internal/multiagent/pi_integration_test.go +++ b/internal/multiagent/pi_integration_test.go @@ -114,6 +114,58 @@ func piTestAgent(cfg *config.Config, handler mcp.ToolHandler) *agent.Agent { return agent.NewAgent(&cfg.OpenAI, &cfg.Agent, srv, nil, zap.NewNop(), 8) } +func TestPiSingleChatAcceptsParameterlessTaskListSchema(t *testing.T) { + cfg := piTestConfig(t) + cfg.SkillsDir = t.TempDir() + cfg.MultiAgent.Runtime.PlantaskEnable = true + cfg.MultiAgent.Skills = config.AgentSkillsConfig{FilesystemTools: new(bool)} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body struct { + Tools []struct { + Function struct { + Name string `json:"name"` + Parameters map[string]any `json:"parameters"` + } `json:"function"` + } `json:"tools"` + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Error(err) + w.WriteHeader(http.StatusBadRequest) + return + } + for _, tool := range body.Tools { + if tool.Function.Name != "TaskList" { + continue + } + if required, exists := tool.Function.Parameters["required"]; exists { + if names, ok := required.([]any); !ok || len(names) != 0 { + http.Error(w, "Invalid schema for function 'TaskList': expected an array of required parameters", http.StatusBadRequest) + return + } + } + piReply(w, "", "", nil, "hello") + return + } + t.Error("TaskList is missing from the request") + w.WriteHeader(http.StatusBadRequest) + })) + defer server.Close() + cfg.OpenAI.BaseURL = server.URL + "/v1" + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + ag := piTestAgent(cfg, func(context.Context, map[string]any) (*mcp.ToolResult, error) { + t.Error("plain chat must not execute the fixture tool") + return nil, fmt.Errorf("unexpected tool call") + }) + res, err := RunSingleAgent(ctx, cfg, &cfg.MultiAgent, ag, nil, zap.NewNop(), "schema-fixture", "", "say hello", nil, []string{"echo"}, nil, nil, "") + if err != nil { + t.Fatal(err) + } + if res.RuntimeStatus != "completed" || res.Response != "hello" { + t.Fatalf("expected a chat reply, got %+v", res) + } +} + func TestPiSinglePreservesHITLAndExecutionEvidence(t *testing.T) { cfg := piTestConfig(t) var calls atomic.Int32 diff --git a/internal/multiagent/pi_runner.go b/internal/multiagent/pi_runner.go index 3bc10df7..ee981abe 100644 --- a/internal/multiagent/pi_runner.go +++ b/internal/multiagent/pi_runner.go @@ -55,7 +55,12 @@ func runPiSingle(ctx context.Context, cfg *config.Config, ag *agent.Agent, db *d } func piToolSchema(properties map[string]any, required ...string) map[string]any { - return map[string]any{"type": "object", "properties": properties, "required": required} + schema := map[string]any{"type": "object", "properties": properties} + // A nil slice would encode as null, which is not a valid required list. + if len(required) > 0 { + schema["required"] = required + } + return schema } func runPiMulti(ctx context.Context, cfg *config.Config, ma *config.MultiAgentConfig, ag *agent.Agent, db *database.DB, logger *zap.Logger,