From 7982c6a3bda4b91da402faf39ca381cabd4832ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BE=8D?= <133481415+icearia0219@users.noreply.github.com> Date: Mon, 12 Oct 2026 13:30:30 +0800 Subject: [PATCH] fix(agent): accept adjacent prefixed Python string literals (#4006) Fixes Tencent/WeKnora#4004 --- internal/agent/tools/python_syntax.go | 6 ++- .../agent/tools/python_syntax_concat_test.go | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 internal/agent/tools/python_syntax_concat_test.go diff --git a/internal/agent/tools/python_syntax.go b/internal/agent/tools/python_syntax.go index 0ed0cce9..ac00974c 100644 --- a/internal/agent/tools/python_syntax.go +++ b/internal/agent/tools/python_syntax.go @@ -93,7 +93,11 @@ func firstBrokenPythonQuote(src string) (int, bool) { for j < len(src) && (src[j] == ' ' || src[j] == '\t' || src[j] == '\r') { j++ } - if ident, isKw := peekPythonIdent(src, j); ident != "" && !isKw { + // Prefixes on an adjacent literal are legal implicit concatenation, + // not a bare identifier after an accidentally closed quote. The next + // loop iteration still scans that literal for unterminated quotes. + _, _, _, _, adjacentLiteral := pythonStringStart(src, j) + if ident, isKw := peekPythonIdent(src, j); ident != "" && !isKw && !adjacentLiteral { return endLine, true } } diff --git a/internal/agent/tools/python_syntax_concat_test.go b/internal/agent/tools/python_syntax_concat_test.go new file mode 100644 index 00000000..35cf5cbf --- /dev/null +++ b/internal/agent/tools/python_syntax_concat_test.go @@ -0,0 +1,54 @@ +package tools + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPythonQuoteCheckAllowsPrefixedAdjacentLiterals(t *testing.T) { + for _, src := range []string{ + `value = "prefix" r"\n"`, + `value = "prefix" u"suffix"`, + `value = "prefix" f"{1 + 2}"`, + `value = "prefix" fr"{1 + 2}\n"`, + `value = "prefix" RF"{1 + 2}\n"`, + `value = b"prefix" br"\n"`, + `value = B"prefix" RB"\n"`, + `value = "prefix"r"suffix"`, + `value = "prefix" r"""suffix"""`, + `value = r"first" u"second" f"{3}"`, + } { + t.Run(src, func(t *testing.T) { + assert.Empty(t, pythonScriptSyntaxHint("/workspace/output/result.py", src, ToolEditSandboxFile)) + }) + } +} + +func TestWritePythonAdjacentLiteralsReportsSuccess(t *testing.T) { + const src = `value = "prefix" r"\n"` + const filePath = "/workspace/output/result.py" + sink := &fakeSandboxFileSink{} + args, err := json.Marshal(map[string]string{"path": filePath, "content": src}) + require.NoError(t, err) + result, err := NewWriteSandboxFileTool(sink, 0).Execute(sandboxFileTestContext(), args) + require.NoError(t, err) + assert.True(t, result.Success, result.Error) + assert.NotContains(t, result.Data, "syntax_error") + assert.Equal(t, src, string(sink.files[filePath])) +} + +func TestPythonQuoteCheckStillScansAdjacentLiteralContents(t *testing.T) { + for _, src := range []string{ + `value = "prefix" r"unterminated`, + `value = "prefix" f"unterminated`, + `value = "prefix" r"suffix"broken`, + `value = "prefix"result`, + } { + t.Run(src, func(t *testing.T) { + assert.NotEmpty(t, pythonScriptSyntaxHint("/workspace/output/result.py", src, ToolEditSandboxFile)) + }) + } +}