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): normalize Python physical newlines before quote checks (#4482)

澍c0b0cebf4ec5
diff --git a/internal/agent/tools/python_syntax.go b/internal/agent/tools/python_syntax.go
index ac00974cb..f0b2f212f 100644
--- a/internal/agent/tools/python_syntax.go
+++ b/internal/agent/tools/python_syntax.go
@@ -64,6 +64,9 @@ func pythonSyntaxErrorHint(stderr string) string {
// is immediately followed by a non-keyword identifier — the parse error
// `"这不是一个"大干快上` produces. `"hello" if x` is left alone.
func firstBrokenPythonQuote(src string) (int, bool) {
+ // Python normalizes physical line endings even inside string literals.
+ // Keep this inspection-only copy consistent with the Python tokenizer.
+ src = strings.ReplaceAll(strings.ReplaceAll(src, "\r\n", "\n"), "\r", "\n")
line := 1
for i := 0; i < len(src); {
switch src[i] {
diff --git a/internal/agent/tools/python_syntax_newlines_test.go b/internal/agent/tools/python_syntax_newlines_test.go
new file mode 100644
index 000000000..0ebf12637
--- /dev/null
+++ b/internal/agent/tools/python_syntax_newlines_test.go
@@ -0,0 +1,40 @@
+package tools
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestPythonQuoteCheckUniversalNewlines(t *testing.T) {
+ for _, newline := range []string{"\n", "\r\n", "\r"} {
+ for _, tc := range []struct {
+ source string
+ line int
+ broken bool
+ }{
+ {"value = \"first\\\nsecond\"\n", 0, false},
+ {"value = 'first\\\nsecond'\n", 0, false},
+ {"value = r\"first\\\nsecond\"\n", 0, false},
+ {"value = f\"first\\\n{2}\"\n", 0, false},
+ {"# a comment\nvalue = \"unterminated\n", 2, true},
+ {"value = \"\"\"first\nsecond\"\"\"\nbad = \"unterminated\n", 3, true},
+ } {
+ t.Run(newline+tc.source, func(t *testing.T) {
+ line, broken := firstBrokenPythonQuote(strings.ReplaceAll(tc.source, "\n", newline))
+ if line != tc.line || broken != tc.broken {
+ t.Fatalf("line ending %q: got (%d, %t), want (%d, %t)",
+ newline, line, broken, tc.line, tc.broken)
+ }
+ })
+ }
+ }
+}
+
+func TestPythonQuoteCheckRejectsUnescapedPhysicalNewlines(t *testing.T) {
+ for _, newline := range []string{"\n", "\r\n", "\r"} {
+ source := "value = \"first" + newline + "second\""
+ if line, broken := firstBrokenPythonQuote(source); !broken || line != 1 {
+ t.Fatalf("expected a broken string on line 1 for %q; got (%d, %t)", newline, line, broken)
+ }
+ }
+}