-
Notifications
You must be signed in to change notification settings - Fork 83
fix(dashboard): supervise next-server for self-heal; sync plugin-root.txt #135
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
Closed
wenchanghan
wants to merge
1
commit into
ReflexioAI:main
from
wenchanghan:fix/dashboard-supervisor-and-plugin-root-sync
+242
−26
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #!/usr/bin/env bash | ||
| # Supervisor for the claude-smart Next.js dashboard. | ||
| # | ||
| # Problem it solves: dashboard-service.sh used to fire `next start` detached and | ||
| # forget about it. When that process died later (a crash, or a macOS jetsam | ||
| # pressure-kill of next-server), nothing restarted it until the next *real* | ||
| # SessionStart hook happened to run `start` again — leaving the dashboard down | ||
| # for as long as Claude Code stayed closed. This wrapper respawns next-server so | ||
| # a silent death self-heals within seconds. | ||
| # | ||
| # Lifecycle: dashboard-service.sh spawn_dashboard() launches this detached as the | ||
| # session leader (setsid) and records THIS pid in dashboard.pid. `stop` | ||
| # group-kills that pid, so the TERM trap below breaks the loop and the | ||
| # next-server child (same process group) is torn down with us — an intentional | ||
| # stop never triggers a respawn. | ||
| # | ||
| # Crash-loop guard: a broken build that starts and immediately exits must not be | ||
| # respawned forever. Exits faster than HEALTHY_SECS are counted as consecutive | ||
| # fast failures; after MAX_FAILS of them the supervisor gives up (exit 1) and | ||
| # leaves recovery to the next SessionStart. Any run that stays up at least | ||
| # HEALTHY_SECS resets the counter, so a long-lived dashboard that eventually | ||
| # dies still self-heals. | ||
| # | ||
| # Note (deliberate limitation): this is an in-process supervisor. A system-wide | ||
| # memory-pressure kill can take the whole process group (supervisor + child) | ||
| # down at once, in which case recovery still falls to the next SessionStart. A | ||
| # launchd/OS-level supervisor would survive that; it is intentionally left as a | ||
| # follow-up. | ||
| # | ||
| # Tunables (env; defaults chosen for production, overridden in tests): | ||
| # CLAUDE_SMART_DASHBOARD_RESPAWN_DELAY seconds to wait between respawns (2) | ||
| # CLAUDE_SMART_DASHBOARD_HEALTHY_SECS uptime that counts as healthy (30) | ||
| # CLAUDE_SMART_DASHBOARD_MAX_FAILS consecutive fast exits allowed (5) | ||
| set -u | ||
|
|
||
| NEXT_BIN="${1:?dashboard-supervise.sh: next binary path required}" | ||
| PORT="${2:?dashboard-supervise.sh: port required}" | ||
|
|
||
| RESPAWN_DELAY="${CLAUDE_SMART_DASHBOARD_RESPAWN_DELAY:-2}" | ||
| HEALTHY_SECS="${CLAUDE_SMART_DASHBOARD_HEALTHY_SECS:-30}" | ||
| MAX_FAILS="${CLAUDE_SMART_DASHBOARD_MAX_FAILS:-5}" | ||
|
|
||
| # On stop (TERM/INT — normally delivered to the whole process group by | ||
| # `dashboard-service.sh stop`) tear down the current next-server and exit the | ||
| # loop instead of respawning. next-server runs in the background and we `wait` | ||
| # on it so the trap fires immediately, even if only this supervisor is signaled. | ||
| child="" | ||
| on_stop() { | ||
| [ -n "$child" ] && kill -TERM "$child" 2>/dev/null | ||
| echo "[claude-smart] dashboard supervisor: received stop signal; exiting" | ||
| exit 0 | ||
| } | ||
| trap on_stop TERM INT | ||
|
|
||
| fails=0 | ||
| while true; do | ||
| started="$(date +%s 2>/dev/null || echo 0)" | ||
| "$NEXT_BIN" start -p "$PORT" -H 127.0.0.1 & | ||
| child=$! | ||
| wait "$child" | ||
| code=$? | ||
| ended="$(date +%s 2>/dev/null || echo 0)" | ||
|
|
||
| # 128+15=143: next-server was SIGTERM'd on its own — normally the port reaper | ||
| # in `dashboard-service.sh stop`/reinstall when it could not see this | ||
| # supervisor's pid (e.g. a stop issued from a different HOME). Respect an | ||
| # intentional stop instead of resurrecting the dashboard. Genuine crashes and | ||
| # jetsam SIGKILLs (137) fall through to the respawn path below. (A stop that | ||
| # *does* see our pid group-kills this supervisor, handled by the trap above.) | ||
| if [ "$code" -eq 143 ]; then | ||
| echo "[claude-smart] dashboard supervisor: next-server received SIGTERM (intentional stop); exiting without respawn" | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [ "$((ended - started))" -ge "$HEALTHY_SECS" ]; then | ||
| fails=0 | ||
| else | ||
| fails=$((fails + 1)) | ||
| fi | ||
|
|
||
| if [ "$fails" -ge "$MAX_FAILS" ]; then | ||
| echo "[claude-smart] dashboard supervisor: next-server exited (code $code); ${MAX_FAILS} fast failures in a row — giving up (recovery deferred to next SessionStart)" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "[claude-smart] dashboard supervisor: next-server exited (code $code); respawning in ${RESPAWN_DELAY}s (consecutive fast failures: $fails)" | ||
| sleep "$RESPAWN_DELAY" | ||
| done | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Use Bash's built-in
$SECONDSto prevent a potential crash-loop bug and avoid subshells.If the environment's
datecommand does not support+%s(e.g., in some lightweight environments), the fallbackecho 0causes bothstartedandendedto evaluate to0. Their difference will always be0, forcing the supervisor to incrementfailson every exit and permanently give up after hittingMAX_FAILS, regardless of how long the dashboard actually stayed up.Using Bash's built-in
$SECONDStracks elapsed time natively, avoiding external process forks and completely eliminating this failure mode.♻️ Proposed refactor
📝 Committable suggestion
🤖 Prompt for AI Agents