From 13abc41b0d3986dc9bdb492fa24735ad00665ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=BB=E4=B9=90=E5=A4=9A?= <865730675@qq.com> Date: Fri, 28 Aug 2026 09:58:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(skill):=20=E8=A7=A3=E6=9E=90=20stringified?= =?UTF-8?q?=20JSON=20=E6=95=B0=E7=BB=84=E5=BD=A2=E5=BC=8F=E7=9A=84=20args?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 部分模型供应商把 execute_skill_script 的 args(schema 为 []string) emit 成 JSON 字符串,内容本身是个 JSON 数组("[\"--project-name\",\"X\"]")。 原 UnmarshalJSON 兜底用 strings.Fields 拆这种字符串,把方括号/引号拆成 垃圾 token,脚本 argv 全是乱码,argparse 永远报参数缺失,模型无论如何 调整格式都失败。改为先把字符串内容当 JSON 数组解析,成功即用;否则再 退回 strings.Fields。补 UnmarshalJSON 各形态单测。 Co-Authored-By: Claude --- internal/agent/tools/skill_execute.go | 15 ++++-- internal/agent/tools/skill_execute_test.go | 53 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/internal/agent/tools/skill_execute.go b/internal/agent/tools/skill_execute.go index 24e5fcfd..0caf01b4 100644 --- a/internal/agent/tools/skill_execute.go +++ b/internal/agent/tools/skill_execute.go @@ -108,9 +108,18 @@ func (i *ExecuteSkillScriptInput) UnmarshalJSON(data []byte) error { return fmt.Errorf("args must be a string or an array of strings: %w", err) } - // A string is interpreted as a conventional space-separated command line. - // The tool schema continues to advertise []string, so well-formed calls are - // unaffected; this is only a compatibility fallback for model output. + // Some providers emit the array as a stringified JSON payload + // (e.g. "[\"--project-name\",\"X\"]"). Treat that as an array first so the + // model's intent is preserved; strings.Fields would otherwise split the + // brackets/quotes into garbage tokens and the script would see nonsense argv. + if err := json.Unmarshal([]byte(argsString), &i.Args); err == nil { + return nil + } + + // A plain string is interpreted as a conventional space-separated command + // line. The tool schema continues to advertise []string, so well-formed + // calls are unaffected; this is only a compatibility fallback for model + // output. i.Args = strings.Fields(argsString) return nil } diff --git a/internal/agent/tools/skill_execute_test.go b/internal/agent/tools/skill_execute_test.go index 2b38fa13..1c67ead5 100644 --- a/internal/agent/tools/skill_execute_test.go +++ b/internal/agent/tools/skill_execute_test.go @@ -1,6 +1,7 @@ package tools import ( + "encoding/json" "testing" "github.com/stretchr/testify/assert" @@ -33,3 +34,55 @@ func TestExecuteSkillScriptDescriptionAcceptsWorkspacePaths(t *testing.T) { assert.Contains(t, executeSkillScriptTool.Description(), "install_deps.py") assert.Contains(t, executeSkillScriptTool.Description(), ".skill-packages") } + +// TestExecuteSkillScriptInputUnmarshalJSON covers the model-emitted shapes the +// UnmarshalJSON fallback must tolerate. The provider does not always honor the +// []string schema: it sometimes emits a stringified JSON array or a single +// command-line string. Each must round-trip to the intended argv. +func TestExecuteSkillScriptInputUnmarshalJSON(t *testing.T) { + t.Run("real array", func(t *testing.T) { + var in ExecuteSkillScriptInput + require.NoError(t, json.Unmarshal([]byte(`{ + "skill_name": "s", "script_path": "p", + "args": ["--project-name", "X", "--creator", "admin"] + }`), &in)) + require.Equal(t, []string{"--project-name", "X", "--creator", "admin"}, in.Args) + }) + + t.Run("stringified json array", func(t *testing.T) { + // Some providers emit args as a JSON string whose content is itself a + // JSON array. strings.Fields would mangle the brackets/quotes into + // one garbage token; the array must be recovered instead. + var in ExecuteSkillScriptInput + require.NoError(t, json.Unmarshal([]byte(`{ + "skill_name": "s", "script_path": "p", + "args": "[\"--project-name\", \"X\", \"--creator\", \"admin\"]" + }`), &in)) + require.Equal(t, []string{"--project-name", "X", "--creator", "admin"}, in.Args) + }) + + t.Run("single command line string", func(t *testing.T) { + var in ExecuteSkillScriptInput + require.NoError(t, json.Unmarshal([]byte(`{ + "skill_name": "s", "script_path": "p", + "args": "ls --workspace /workspace/output" + }`), &in)) + require.Equal(t, []string{"ls", "--workspace", "/workspace/output"}, in.Args) + }) + + t.Run("absent args", func(t *testing.T) { + var in ExecuteSkillScriptInput + require.NoError(t, json.Unmarshal([]byte(`{ + "skill_name": "s", "script_path": "p" + }`), &in)) + require.Empty(t, in.Args) + }) + + t.Run("null args", func(t *testing.T) { + var in ExecuteSkillScriptInput + require.NoError(t, json.Unmarshal([]byte(`{ + "skill_name": "s", "script_path": "p", "args": null + }`), &in)) + require.Empty(t, in.Args) + }) +}