Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

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

澍7982c6a3bda4
diff --git a/internal/agent/tools/python_syntax.go b/internal/agent/tools/python_syntax.go
index 0ed0cce90..ac00974cb 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 000000000..35cf5cbfd
--- /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))
+ })
+ }
+}