cmd_delegate: race condition causes msg file to be deleted before opencode reads it #210
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
cmd_delegate deletes the message file immediately after spawning the background process, causing a race condition where opencode run may not have read the file yet.
Error
Error: File not found: /home/shoko/.kugetsu/logs/msg-ses_298b8ad7fffep3yh1QQDuyi6tz.txt
Root Cause
In cmd_delegate (kugetsu-session.sh:231):
printf %s "$message" > "$msg_file"
nohup sh -c "opencode run @ --session " >> "$log_file" 2>&1 &
rm -f "$msg_file" # Runs immediately after nohup, before opencode reads file!
The rm -f $msg_file executes right after nohup spawns the background process, but opencode run has not necessarily read the file yet.
Fix
Remove rm -f $msg_file from cmd_delegate, consistent with how cmd_start and cmd_continue handle message files (they write to $worktree_path/.kugetsu-msg.txt and never delete it).
Orphaned msg files in $LOGS_DIR/ are harmless and get cleaned up by log rotation in cmd_logs.