fix(agent): accept adjacent prefixed Python string literals (#4006)

Fixes Tencent/WeKnora#4004
main
澍 2026-10-12 13:30:30 +08:00 committed by GitHub
parent 9b031a6576
commit 7982c6a3bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 1 deletions

View File

@ -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
}
}

View File

@ -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))
})
}
}