summaryrefslogtreecommitdiffstats
path: root/lexd-spool.sh
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2026-09-02 06:59:24 +0800
committerSadeep Madurange <sadeep@asciimx.com>2026-09-02 06:59:24 +0800
commitb904341947bfeac99fe91d3c9ace760f7da312fd (patch)
tree1c9b665efca6d396ed707fb8dea4bba119fc3155 /lexd-spool.sh
parent4c1da52f4a7bb71952fa2eaa06b230e9f86ea699 (diff)
downloadlex-b904341947bfeac99fe91d3c9ace760f7da312fd.tar.gz
Quarantine bad spool files, toss leading < from llm response.
Diffstat (limited to 'lexd-spool.sh')
-rw-r--r--[-rwxr-xr-x]lexd-spool.sh35
1 files changed, 31 insertions, 4 deletions
diff --git a/lexd-spool.sh b/lexd-spool.sh
index c0cf632..9b9b723 100755..100644
--- a/lexd-spool.sh
+++ b/lexd-spool.sh
@@ -9,6 +9,7 @@ SSH_HOST="lex-spool"
TMP_DIR="/var/www/var/lex/tmp"
DICT_DIR="/var/www/var/lex/dict"
SPOOL_DIR="/var/www/var/lex/spool"
+BAD_DIR="/var/www/var/lex/bad"
SOCK_PATH="/var/run/lexd/sock"
SLEEP_INTERVAL=30
@@ -20,8 +21,15 @@ log_info() {
logger -t lexd-spool -p daemon.info "$1"
}
+# Move an unprocessable or corrupt file to the remote bad directory
+quarantine_file() {
+ log_err "Moving unprocessable file to ${SPOOL_FILE}"
+ ssh -F "${SSH_CONFIG}" "${SSH_HOST}" \
+ "mkdir -p '${BAD_DIR}' && mv -f '${SPOOL_DIR}/${SPOOL_FILE}' '${BAD_DIR}/'" 2>/dev/null
+}
+
while true; do
- # Fetch one file
+ # Fetch one file cleanly from spool directory
SPOOL_FILE=$(ssh -F "${SSH_CONFIG}" -n "${SSH_HOST}" \
"ls -1 '${SPOOL_DIR}' 2>/dev/null | head -n 1")
@@ -46,12 +54,23 @@ while true; do
if [ -z "${raw_prompt}" ]; then
log_err "Empty spool file: ${SPOOL_FILE}"
+ quarantine_file
exit 1
fi
- # Log truncated input string (up to 45 characters)
- trunc_prompt=$(print -r -- "${raw_prompt}" | cut -c1-45)
- log_info "Processing: \"${trunc_prompt}\""
+ # Log spool file + prompt snippet, capped at 45 characters total
+ prefix="${SPOOL_FILE}: "
+ prefix_len=${#prefix}
+ max_prompt_len=$(( 45 - prefix_len ))
+
+ if [ ${max_prompt_len} -gt 0 ]; then
+ trunc_prompt=$(print -r -- "${raw_prompt}" | cut -c1-"${max_prompt_len}")
+ log_entry="${prefix}${trunc_prompt}"
+ else
+ log_entry=$(print -r -- "${prefix}" | cut -c1-45)
+ fi
+
+ log_info "Processing: ${log_entry}"
# Pass prompt to socket and insert missing empty lines before uppercase headers
response=$(print -r -- "${raw_prompt}" | nc -w 300 -U "${SOCK_PATH}" | awk '
@@ -68,15 +87,20 @@ while true; do
if [ -z "${response}" ]; then
log_err "Empty socket response for: ${SPOOL_FILE}"
+ quarantine_file
exit 1
fi
+ # Strip leading '<' character if present
+ response="${response#<}"
+
# Extract first word (lowercased)
target_word=$(print -r -- "${response}" \
| awk '{print tolower($1); exit}')
if [ -z "${target_word}" ]; then
log_err "No target filename for: ${SPOOL_FILE}"
+ quarantine_file
exit 1
fi
@@ -84,10 +108,12 @@ while true; do
case "${target_word}" in
*[!a-z-]*)
log_err "Invalid word '${target_word}': ${SPOOL_FILE}"
+ quarantine_file
exit 1
;;
[!a-z]*|*[!a-z])
log_err "Bad bounds '${target_word}': ${SPOOL_FILE}"
+ quarantine_file
exit 1
;;
esac
@@ -108,6 +134,7 @@ while true; do
if [ ${ssh_res} -ne 0 ]; then
log_err "SSH error on ${SPOOL_FILE} (code ${ssh_res}): ${ssh_err}"
+ quarantine_file
exit 1
fi