-
Notifications
You must be signed in to change notification settings - Fork 486
fix: validate settings before stopping daemon on restart #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -267,8 +267,54 @@ stop_daemon() { | |||||||||||||||||||||||||||||||
| log "Daemon stopped" | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Validate settings without starting the daemon. | ||||||||||||||||||||||||||||||||
| # Returns 0 if ready to start, non-zero otherwise. | ||||||||||||||||||||||||||||||||
| # Safe to call while the daemon is running — reads only, no side effects. | ||||||||||||||||||||||||||||||||
| preflight_check() { | ||||||||||||||||||||||||||||||||
| load_settings | ||||||||||||||||||||||||||||||||
| local load_rc=$? | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if [ $load_rc -eq 2 ]; then | ||||||||||||||||||||||||||||||||
| local jq_err | ||||||||||||||||||||||||||||||||
| jq_err=$(jq empty "$SETTINGS_FILE" 2>&1) | ||||||||||||||||||||||||||||||||
| echo -e "${RED}Restart aborted: settings.json contains invalid JSON${NC}" | ||||||||||||||||||||||||||||||||
| echo -e " ${YELLOW}${jq_err}${NC}" | ||||||||||||||||||||||||||||||||
| echo " Fix manually: $SETTINGS_FILE" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
|
Comment on lines
+277
to
+283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Auto-repair path bypassed and error message is incomplete
Consider either:
Suggested change
|
||||||||||||||||||||||||||||||||
| elif [ $load_rc -ne 0 ]; then | ||||||||||||||||||||||||||||||||
| echo -e "${RED}Restart aborted: settings.json is missing or has no configuration${NC}" | ||||||||||||||||||||||||||||||||
| echo " Run 'tinyclaw setup' to reconfigure" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if [ ${#ACTIVE_CHANNELS[@]} -eq 0 ]; then | ||||||||||||||||||||||||||||||||
| echo -e "${RED}Restart aborted: no channels configured in settings.json${NC}" | ||||||||||||||||||||||||||||||||
| echo " Run 'tinyclaw setup' to reconfigure" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for ch in "${ACTIVE_CHANNELS[@]}"; do | ||||||||||||||||||||||||||||||||
| local token_key | ||||||||||||||||||||||||||||||||
| token_key="$(channel_token_key "$ch")" | ||||||||||||||||||||||||||||||||
| if [ -n "$token_key" ] && [ -z "$(get_channel_token "$ch")" ]; then | ||||||||||||||||||||||||||||||||
| echo -e "${RED}Restart aborted: $(channel_display "$ch") bot token is missing${NC}" | ||||||||||||||||||||||||||||||||
| echo " Run 'tinyclaw setup' to reconfigure" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Restart daemon safely even when called from inside TinyClaw's tmux session | ||||||||||||||||||||||||||||||||
| restart_daemon() { | ||||||||||||||||||||||||||||||||
| # Validate settings before touching the live session. | ||||||||||||||||||||||||||||||||
| # If preflight fails, the running daemon stays up and we bail out. | ||||||||||||||||||||||||||||||||
| if ! preflight_check; then | ||||||||||||||||||||||||||||||||
| echo -e "${YELLOW}Daemon restart cancelled — live session preserved${NC}" | ||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if session_exists && [ -n "${TMUX:-}" ]; then | ||||||||||||||||||||||||||||||||
| local current_session | ||||||||||||||||||||||||||||||||
| current_session=$(tmux display-message -p '#S' 2>/dev/null || true) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misleading "no side effects" comment
The comment claims
preflight_checkis "reads only, no side effects", butload_settings()(called on line 274) actually modifies several global variables:ACTIVE_CHANNELS,WORKSPACE_PATH,_CHANNEL_TOKEN_KEYS, and_CHANNEL_TOKEN_VALS.This is currently benign —
start_daemon()callsload_settings()again immediately after and reinitialises these globals — but the comment could mislead a future developer into callingpreflight_check()from a context where those globals must remain untouched.