Route large unclosed drafts to edit

ober

4a6a0b5522f1fad1ed0787dc4d782cd6079dbf7e

diff --git a/src/jcode/core/verified-run.ss b/src/jcode/core/verified-run.ss
index a13ab4e..1dbace6 100644
--- a/src/jcode/core/verified-run.ss
+++ b/src/jcode/core/verified-run.ss
@@ -451,14 +451,21 @@
        (openai-function-tool-choice "edit")))
 
 (def (rejected-draft-direct-edit-guard?)
-  (let ((msg (current-rejected-draft-guard-message)))
+  (let ((msg (current-rejected-draft-guard-message))
+        (draft (current-rejected-ss-draft)))
     (and (string? msg)
          (or (string-contains msg "list-set! is not available")
              (string-contains msg "random-make-state")
              (string-contains msg "Jerboa has no return form")
              (string-contains msg "Jerboa has no break form")
              (string-contains msg "Jerboa has no continue form")
-             (string-contains msg "Jerboa imports use")))))
+             (string-contains msg "Jerboa imports use")
+             (and (pair? draft)
+                  (> (string-length (cdr draft)) 4096)
+                  (or (string-contains msg
+                        "delimiter balance failed: Unclosed")
+                      (string-contains msg
+                        "Minimal EOF delimiter repair was not applied")))))))
 
 (def (force-staged-create-repair-tool-name specs)
   ;; Syntax-broken missing-file creates have no on-disk artifact yet. The
diff --git a/test/run.ss b/test/run.ss
index 470d4a8..07abcd4 100644
--- a/test/run.ss
+++ b/test/run.ss
@@ -9232,6 +9232,60 @@
 			  (safe-delete-test-file! target-path))
 
 		(let* ([vr-dir  "/tmp"]
+		       [target "jcode-local-unclosed-create-direct-edit.ss"]
+		       [target-path (string-append vr-dir "/" target)]
+		       [padding (make-string 5000 #\space)]
+		       [broken (string-append
+		                 "(import (jerboa prelude))\n(def (main)\n  (displayln \"fixed\")\n(def after #t)\n"
+		                 padding)]
+		       [fixed "(import (jerboa prelude))\n(def (main)\n  (displayln \"fixed\"))\n(main)\n"]
+		       [tool-names-after-reject '()]
+		       [calls 0]
+		       [slurp (lambda (p)
+		                (call-with-input-file p
+		                  (lambda (in) (get-string-all in))))]
+		       [scope (parse-write-scope target)]
+		       [provider
+		         (lambda (_messages tool-specs _step)
+		           (set! calls (+ calls 1))
+		           (case calls
+		             [(1)
+		              (list (make-wtool-call "write"
+		                      (list (cons "path" target)
+		                            (cons "content" broken)) #f))]
+		             [(2)
+		              (set! tool-names-after-reject
+		                (map tool-spec-name tool-specs))
+		              (list (make-wtool-call "edit"
+		                      (list (cons "path" target)
+		                            (cons "content" fixed)) #f))]
+		             [(3)
+		              (list (make-wtool-call "verify" '() #f))]
+		             [else
+		              (error 'test "unexpected local unclosed create step")]))]
+		       [result (begin
+		                 (safe-delete-test-file! target-path)
+		                 (verified-run provider
+		                   "direct-edit local unclosed create"
+		                   (list (cons 'cwd vr-dir)
+		                         (cons 'verify-command
+		                               (string-append "grep -q fixed " target))
+		                         (cons 'write-scope scope)
+		                         (cons 'local-model? #t)
+		                         (cons 'max-iterations 6)
+		                         (cons 'max-tool-errors 2))))])
+		  (check! "verified-run: local unclosed create direct edit verifies"
+		          result "VERIFIED: exit 0\n")
+		  (check! "verified-run: local unclosed create direct edit writes file"
+		          (slurp target-path) fixed)
+		  (check-pred! "verified-run: local unclosed create avoids forced balance"
+		    tool-names-after-reject
+		    (lambda (names)
+		      (and (equal? names '("edit"))
+		           (not (member "balance" names)))))
+		  (safe-delete-test-file! target-path))
+
+		(let* ([vr-dir  "/tmp"]
 		       [target "jcode-verified-hidden-write-after-syntax-create.ss"]
 		       [target-path (string-append vr-dir "/" target)]
 		       [broken "(import (jerboa prelude))\n(def (main)\n  (list-set! xs 0 #t)\n  (displayln \"fixed\"))\n(main)\n"]