TUI+agent: word-wrap on spaces; survive a dead cwd; fix repomap crash

ober

858ab10a48aa53c4ddda706ecca66d19c288e745

diff --git a/main.ss b/main.ss
index 1ce56a6..c3d8cdc 100644
--- a/main.ss
+++ b/main.ss
@@ -6,4 +6,11 @@
         (jcode core agent)
         (jcode ui cli))
 
+;; Repair a dead/inaccessible process cwd before anything calls
+;; (current-directory): Chez's getcwd raises "cannot determine current
+;; directory" and the unguarded getter kills the agent at startup. Probe
+;; it, and on failure chdir to $HOME (then /) to restore a valid cwd.
+(guard (e (#t (current-directory (or (getenv "HOME") "/"))))
+  (current-directory))
+
 (cli-main (command-line-arguments))
diff --git a/src/jcode/core/repomap.ss b/src/jcode/core/repomap.ss
index 9069a00..c3f57a4 100644
--- a/src/jcode/core/repomap.ss
+++ b/src/jcode/core/repomap.ss
@@ -277,13 +277,14 @@
             (collect-file-refs file defining-file))))
       defs)
     ;; Build in-edges and run pagerank.
-    (hash-for-each out-edges
+    (hash-for-each
       (lambda (src targets)
         (for-each
           (lambda (tgt)
             (hash-put! in-edges tgt
               (cons src (or (hash-get in-edges tgt) '()))))
-          targets)))
+          targets))
+      out-edges)
     (let* ((all-files (map car defs))
            (scores (pagerank all-files out-edges 0.85 25))
            (ranked (list-sort (lambda (a b) (> (cdr a) (cdr b))) scores)))
@@ -304,12 +305,13 @@
           (let loop ()
             (let ((line (get-line p)))
               (unless (eof-object? line)
-                (hash-for-each defining-file
+                (hash-for-each
                   (lambda (sym owner)
                     (when (and (not (equal? owner file))
                                (not (hash-ref seen owner #f))
                                (string-contains line sym))
-                      (hash-put! seen owner #t))))
+                      (hash-put! seen owner #t)))
+                  defining-file)
                 (loop))))))
       (catch (e) #f))
     (hash-keys seen)))
diff --git a/src/jcode/ui/tui-message.ss b/src/jcode/ui/tui-message.ss
index 96f3b37..d01d91d 100644
--- a/src/jcode/ui/tui-message.ss
+++ b/src/jcode/ui/tui-message.ss
@@ -117,9 +117,19 @@
                           (cons (cons (substring text pos len) face) cur-line)
                           (+ col remaining) out))
                    (else
-                    ;; Fill current line, start new one
-                    (let ((chunk (substring text pos (+ pos avail))))
-                      (break (+ pos avail)
+                    ;; Break at the last space within the available width so
+                    ;; words aren't split mid-character ("review fo" | "r bugs").
+                    ;; Fall back to a hard char-break only when a single token
+                    ;; is itself wider than the line, so we always progress.
+                    (let* ((hard (+ pos avail))
+                           (brk (let scan ((i (- hard 1)))
+                                  (cond ((<= i pos) #f)
+                                        ((char=? (string-ref text i) #\space) i)
+                                        (else (scan (- i 1))))))
+                           (cut (or brk hard))
+                           (chunk (substring text pos cut))
+                           (next (if brk (+ cut 1) cut)))
+                      (break next
                              '()
                              0
                              (cons (reverse (cons (cons chunk face) cur-line)) out))))))))))))))