From d2eaf806c9fb863934fd073f154a002d23b21841 Mon Sep 17 00:00:00 2001 From: Leul Negash Date: Mon, 13 Jul 2026 07:48:11 +0300 Subject: [PATCH] Fix append-file concatenating without a newline (#204) append-file appends a line but only guarantees a newline after the text, never before it. When the file does not already end with a newline, which is what the byte-exact write-file leaves behind, the appended line does not start on a new line and the contents are silently merged. Add line_separator/2 to emit a newline before appending when the file does not already end at a line start. write-file remains byte-exact. --- src/skills.metta | 16 +++++++++------- src/skills.pl | 10 ++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/skills.metta b/src/skills.metta index 355284f4..3c850a30 100644 --- a/src/skills.metta +++ b/src/skills.metta @@ -41,12 +41,14 @@ WRITE-FILE-SUCCESS)) (= (append-file $file $str) - (progn (translatePredicate (exists_file $file)) - (translatePredicate (open $file append $Out)) - (translatePredicate (write $Out $str)) - (translatePredicate (nl $Out)), - (translatePredicate (close $Out)) - APPEND-FILE-SUCCESS)) + (let $sep (line_separator $file) + (progn (translatePredicate (exists_file $file)) + (translatePredicate (open $file append $Out)) + (translatePredicate (write $Out $sep)) + (translatePredicate (write $Out $str)) + (translatePredicate (nl $Out)) + (translatePredicate (close $Out)) + APPEND-FILE-SUCCESS))) (= (tavily-search $query) (py-call (agentverse.tavily_search $query))) @@ -57,7 +59,7 @@ (= (websearch $msg) (py-call (websearch.search $msg))) -!(import_prolog_functions_from_file (library OmegaClaw-Core ./src/skills.pl) (shell first_char gc read_file_tail)) +!(import_prolog_functions_from_file (library OmegaClaw-Core ./src/skills.pl) (shell first_char gc read_file_tail line_separator)) (= (metta $str) (let $code (sread $str) diff --git a/src/skills.pl b/src/skills.pl index 6afef19f..945d3c1b 100644 --- a/src/skills.pl +++ b/src/skills.pl @@ -54,3 +54,13 @@ ), close(In) ). + +%Newline needed to start a new line at the end of Path, empty if already at one: +line_separator(Path, Sep) :- + ( exists_file(Path), + read_file_tail(Path, 1, Tail), + Tail \== "", + Tail \== "\n" + -> Sep = "\n" + ; Sep = "" + ).