@ddtcorex/dsh-maestro-core 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +209 -0
- package/cordis.patch.yml +43 -0
- package/lib/bin.d.ts +2 -0
- package/lib/bin.js +3 -0
- package/lib/boot-guard.d.ts +33 -0
- package/lib/boot-guard.js +68 -0
- package/lib/boot-lock.d.ts +56 -0
- package/lib/boot-lock.js +155 -0
- package/lib/cli.d.ts +31 -0
- package/lib/cli.js +336 -0
- package/lib/client.js +2901 -0
- package/lib/config/index.d.ts +34 -0
- package/lib/config/index.js +62 -0
- package/lib/config/service.d.ts +18 -0
- package/lib/config/service.js +28 -0
- package/lib/config.d.ts +1 -0
- package/lib/config.js +19 -0
- package/lib/daemon-freshness.d.ts +48 -0
- package/lib/daemon-freshness.js +122 -0
- package/lib/debug-agent.d.ts +17 -0
- package/lib/debug-agent.js +144 -0
- package/lib/dsh-session.d.ts +26 -0
- package/lib/dsh-session.js +45 -0
- package/lib/guard/config.d.ts +82 -0
- package/lib/guard/config.js +211 -0
- package/lib/guard/decide.d.ts +30 -0
- package/lib/guard/decide.js +38 -0
- package/lib/guard/full-scan-tool.d.ts +6 -0
- package/lib/guard/full-scan-tool.js +65 -0
- package/lib/guard/index.d.ts +85 -0
- package/lib/guard/index.js +254 -0
- package/lib/guard/journal.d.ts +146 -0
- package/lib/guard/journal.js +398 -0
- package/lib/guard/migrate.d.ts +15 -0
- package/lib/guard/migrate.js +42 -0
- package/lib/guard/parse.d.ts +193 -0
- package/lib/guard/parse.js +1103 -0
- package/lib/guard/paths.d.ts +104 -0
- package/lib/guard/paths.js +254 -0
- package/lib/guard/permission-policy.d.ts +10 -0
- package/lib/guard/permission-policy.js +21 -0
- package/lib/guard/redact.d.ts +25 -0
- package/lib/guard/redact.js +52 -0
- package/lib/guard/rules.d.ts +65 -0
- package/lib/guard/rules.js +832 -0
- package/lib/guard/status-tool.d.ts +54 -0
- package/lib/guard/status-tool.js +195 -0
- package/lib/guard/tiers.d.ts +2 -0
- package/lib/guard/tiers.js +1 -0
- package/lib/guard/validators.d.ts +4 -0
- package/lib/guard/validators.js +45 -0
- package/lib/health-poller.d.ts +90 -0
- package/lib/health-poller.js +430 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +7 -0
- package/lib/intents.d.ts +32 -0
- package/lib/intents.js +49 -0
- package/lib/notifier.d.ts +12 -0
- package/lib/notifier.js +62 -0
- package/lib/paths.d.ts +13 -0
- package/lib/paths.js +70 -0
- package/lib/plugin.d.ts +250 -0
- package/lib/plugin.js +1126 -0
- package/lib/preset.d.ts +73 -0
- package/lib/preset.js +158 -0
- package/lib/report.d.ts +11 -0
- package/lib/report.js +58 -0
- package/lib/restart-exec.d.ts +35 -0
- package/lib/restart-exec.js +60 -0
- package/lib/restart-guards.d.ts +39 -0
- package/lib/restart-guards.js +150 -0
- package/lib/restart-tool.d.ts +124 -0
- package/lib/restart-tool.js +539 -0
- package/lib/restart-web.d.ts +26 -0
- package/lib/restart-web.js +58 -0
- package/lib/resume-log.d.ts +24 -0
- package/lib/resume-log.js +18 -0
- package/lib/resume-tools.d.ts +131 -0
- package/lib/resume-tools.js +337 -0
- package/lib/resume.d.ts +33 -0
- package/lib/resume.js +221 -0
- package/lib/scan.d.ts +20 -0
- package/lib/scan.js +71 -0
- package/lib/self-kill-guard.d.ts +81 -0
- package/lib/self-kill-guard.js +208 -0
- package/lib/session-health.d.ts +89 -0
- package/lib/session-health.js +274 -0
- package/lib/session-log-file.d.ts +22 -0
- package/lib/session-log-file.js +74 -0
- package/lib/skill-provider.d.ts +6 -0
- package/lib/skill-provider.js +73 -0
- package/lib/snapshot.d.ts +59 -0
- package/lib/snapshot.js +358 -0
- package/lib/source.d.ts +19 -0
- package/lib/source.js +19 -0
- package/lib/store/index.d.ts +42 -0
- package/lib/store/index.js +340 -0
- package/lib/store/legacy.d.ts +17 -0
- package/lib/store/legacy.js +85 -0
- package/lib/supervisor.d.ts +119 -0
- package/lib/supervisor.js +741 -0
- package/lib/sync/backup-config.d.ts +55 -0
- package/lib/sync/backup-config.js +119 -0
- package/lib/sync/backup-service.d.ts +110 -0
- package/lib/sync/backup-service.js +440 -0
- package/lib/sync/bidirectional.d.ts +39 -0
- package/lib/sync/bidirectional.js +25 -0
- package/lib/sync/cli.d.ts +38 -0
- package/lib/sync/cli.js +536 -0
- package/lib/sync/config.d.ts +77 -0
- package/lib/sync/config.js +90 -0
- package/lib/sync/fingerprint.d.ts +29 -0
- package/lib/sync/fingerprint.js +65 -0
- package/lib/sync/hashing.d.ts +9 -0
- package/lib/sync/hashing.js +47 -0
- package/lib/sync/index.d.ts +6 -0
- package/lib/sync/index.js +634 -0
- package/lib/sync/machine-id.d.ts +29 -0
- package/lib/sync/machine-id.js +66 -0
- package/lib/sync/merge.d.ts +33 -0
- package/lib/sync/merge.js +88 -0
- package/lib/sync/peer-host.d.ts +22 -0
- package/lib/sync/peer-host.js +62 -0
- package/lib/sync/process-runner.d.ts +31 -0
- package/lib/sync/process-runner.js +161 -0
- package/lib/sync/remote-agent.d.ts +42 -0
- package/lib/sync/remote-agent.js +217 -0
- package/lib/sync/remote-cache.d.ts +18 -0
- package/lib/sync/remote-cache.js +60 -0
- package/lib/sync/remote-manifest.d.ts +8 -0
- package/lib/sync/remote-manifest.js +64 -0
- package/lib/sync/s3-object-store.d.ts +38 -0
- package/lib/sync/s3-object-store.js +126 -0
- package/lib/sync/session-merge.d.ts +18 -0
- package/lib/sync/session-merge.js +191 -0
- package/lib/sync/session-plan.d.ts +29 -0
- package/lib/sync/session-plan.js +213 -0
- package/lib/sync/sigv4.d.ts +20 -0
- package/lib/sync/sigv4.js +57 -0
- package/lib/sync/snapshot.d.ts +21 -0
- package/lib/sync/snapshot.js +176 -0
- package/lib/sync/ssh-mux.d.ts +12 -0
- package/lib/sync/ssh-mux.js +48 -0
- package/lib/sync/sync-plan.d.ts +24 -0
- package/lib/sync/sync-plan.js +366 -0
- package/lib/sync/sync-service.d.ts +177 -0
- package/lib/sync/sync-service.js +819 -0
- package/lib/sync/sync-types.d.ts +87 -0
- package/lib/sync/sync-types.js +1 -0
- package/lib/sync/transport.d.ts +75 -0
- package/lib/sync/transport.js +200 -0
- package/lib/sync/tunnel-restore.d.ts +77 -0
- package/lib/sync/tunnel-restore.js +142 -0
- package/lib/sync/validation.d.ts +10 -0
- package/lib/sync/validation.js +83 -0
- package/lib/tool-view.d.ts +62 -0
- package/lib/tool-view.js +70 -0
- package/lib/types/client/auto-reload.d.ts +8 -0
- package/lib/types/client/auto-reload.d.ts.map +1 -0
- package/lib/types/client/config/MaestroSettings.d.ts +25 -0
- package/lib/types/client/config/MaestroSettings.d.ts.map +1 -0
- package/lib/types/client/config/components/BrandMark.d.ts +50 -0
- package/lib/types/client/config/components/BrandMark.d.ts.map +1 -0
- package/lib/types/client/config/guard-view.d.ts +83 -0
- package/lib/types/client/config/guard-view.d.ts.map +1 -0
- package/lib/types/client/config/index.d.ts +20 -0
- package/lib/types/client/config/index.d.ts.map +1 -0
- package/lib/types/client/config/maestro-mark.d.ts +29 -0
- package/lib/types/client/config/maestro-mark.d.ts.map +1 -0
- package/lib/types/client/config/settings-nav-icon.d.ts +21 -0
- package/lib/types/client/config/settings-nav-icon.d.ts.map +1 -0
- package/lib/types/client/index.d.ts +16 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts +6 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/SyncPanel.d.ts +12 -0
- package/lib/types/client/sync/SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/confirm-dialog.d.ts +43 -0
- package/lib/types/client/sync/confirm-dialog.d.ts.map +1 -0
- package/lib/types/client/sync/index.d.ts +10 -0
- package/lib/types/client/sync/index.d.ts.map +1 -0
- package/lib/types/client/sync/ui.d.ts +79 -0
- package/lib/types/client/sync/ui.d.ts.map +1 -0
- package/lib/types/client/sync/use-backup.d.ts +50 -0
- package/lib/types/client/sync/use-backup.d.ts.map +1 -0
- package/lib/types/client/sync/use-sync.d.ts +88 -0
- package/lib/types/client/sync/use-sync.d.ts.map +1 -0
- package/package.json +89 -0
- package/skills/dsh-safe-restart/SKILL.md +206 -0
- package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +468 -0
- package/src/client/auto-reload.ts +104 -0
- package/src/client/config/MaestroSettings.tsx +892 -0
- package/src/client/config/components/BrandMark.tsx +94 -0
- package/src/client/config/guard-view.ts +187 -0
- package/src/client/config/index.tsx +109 -0
- package/src/client/config/maestro-mark.ts +37 -0
- package/src/client/config/settings-nav-icon.ts +68 -0
- package/src/client/index.tsx +33 -0
- package/src/client/sync/R2SyncPanel.tsx +260 -0
- package/src/client/sync/SyncPanel.tsx +422 -0
- package/src/client/sync/confirm-dialog.tsx +202 -0
- package/src/client/sync/index.tsx +336 -0
- package/src/client/sync/ui.tsx +313 -0
- package/src/client/sync/use-backup.ts +207 -0
- package/src/client/sync/use-sync.ts +502 -0
- package/src/host/bin.ts +3 -0
- package/src/host/boot-guard.ts +76 -0
- package/src/host/boot-lock.ts +155 -0
- package/src/host/cli.ts +288 -0
- package/src/host/config/index.ts +78 -0
- package/src/host/config/service.ts +40 -0
- package/src/host/config.ts +18 -0
- package/src/host/daemon-freshness.ts +143 -0
- package/src/host/debug-agent.ts +145 -0
- package/src/host/dsh-session.ts +56 -0
- package/src/host/dsh-skill.d.ts +44 -0
- package/src/host/guard/augment.d.ts +33 -0
- package/src/host/guard/config.ts +244 -0
- package/src/host/guard/decide.ts +40 -0
- package/src/host/guard/full-scan-tool.ts +61 -0
- package/src/host/guard/index.ts +281 -0
- package/src/host/guard/journal.ts +440 -0
- package/src/host/guard/migrate.ts +41 -0
- package/src/host/guard/parse.ts +1150 -0
- package/src/host/guard/paths.ts +239 -0
- package/src/host/guard/permission-policy.ts +15 -0
- package/src/host/guard/redact.ts +64 -0
- package/src/host/guard/rules.ts +891 -0
- package/src/host/guard/status-tool.ts +229 -0
- package/src/host/guard/tiers.ts +2 -0
- package/src/host/guard/validators.ts +29 -0
- package/src/host/health-poller.ts +446 -0
- package/src/host/index.ts +7 -0
- package/src/host/intents.ts +67 -0
- package/src/host/notifier.ts +71 -0
- package/src/host/paths.ts +70 -0
- package/src/host/plugin.ts +1097 -0
- package/src/host/preset.ts +170 -0
- package/src/host/report.ts +71 -0
- package/src/host/restart-exec.ts +81 -0
- package/src/host/restart-guards.ts +150 -0
- package/src/host/restart-tool.ts +483 -0
- package/src/host/restart-web.ts +72 -0
- package/src/host/resume-log.ts +39 -0
- package/src/host/resume-tools.ts +385 -0
- package/src/host/resume.ts +204 -0
- package/src/host/scan.ts +61 -0
- package/src/host/self-kill-guard.ts +216 -0
- package/src/host/session-health.ts +236 -0
- package/src/host/session-log-file.ts +77 -0
- package/src/host/skill-provider.ts +69 -0
- package/src/host/snapshot.ts +337 -0
- package/src/host/source.ts +19 -0
- package/src/host/store/index.ts +348 -0
- package/src/host/store/legacy.ts +96 -0
- package/src/host/supervisor.ts +720 -0
- package/src/host/sync/backup-config.ts +159 -0
- package/src/host/sync/backup-service.ts +453 -0
- package/src/host/sync/bidirectional.ts +66 -0
- package/src/host/sync/cli.ts +539 -0
- package/src/host/sync/config.ts +119 -0
- package/src/host/sync/fingerprint.ts +83 -0
- package/src/host/sync/hashing.ts +58 -0
- package/src/host/sync/index.ts +737 -0
- package/src/host/sync/machine-id.ts +81 -0
- package/src/host/sync/merge.ts +102 -0
- package/src/host/sync/peer-host.ts +65 -0
- package/src/host/sync/process-runner.ts +177 -0
- package/src/host/sync/remote-agent.ts +213 -0
- package/src/host/sync/remote-cache.ts +68 -0
- package/src/host/sync/remote-manifest.ts +70 -0
- package/src/host/sync/s3-object-store.ts +144 -0
- package/src/host/sync/session-merge.ts +220 -0
- package/src/host/sync/session-plan.ts +237 -0
- package/src/host/sync/sigv4.ts +81 -0
- package/src/host/sync/snapshot.ts +162 -0
- package/src/host/sync/ssh-mux.ts +56 -0
- package/src/host/sync/sync-plan.ts +366 -0
- package/src/host/sync/sync-service.ts +855 -0
- package/src/host/sync/sync-types.ts +102 -0
- package/src/host/sync/transport.ts +261 -0
- package/src/host/sync/tunnel-restore.ts +160 -0
- package/src/host/sync/validation.ts +91 -0
- package/src/host/tool-view.ts +98 -0
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Restart the DSH Web process tree only after the caller has obtained consent.
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
|
|
5
|
+
repo="${DSH_REPO:-}"
|
|
6
|
+
log="${DSH_RESTART_LOG:-/tmp/dsh-web-restart.log}"
|
|
7
|
+
# Coordination with dsh-web-supervisor (see workspace docs/specs/
|
|
8
|
+
# 2026-08-28-supervisor-planned-restart-design.md): the supervisor treats a
|
|
9
|
+
# down poll as a crash unless this marker is fresh, so it never races this
|
|
10
|
+
# script's own kill -> dry-boot -> relaunch sequence with its own rollback.
|
|
11
|
+
marker="${DSH_SUPERVISOR_MARKER:-$HOME/.dsh/.supervisor/planned-restart}"
|
|
12
|
+
# Session-log pre-flight env (see lib/session-health.ts): SESSIONS_ROOT is the
|
|
13
|
+
# operator DSH store's sessions dir (default <dsh home>/sessions) and may be
|
|
14
|
+
# overridden per invocation. PLUGIN_DIR is this package's root, derived from
|
|
15
|
+
# the script's own path — never a hard-coded location.
|
|
16
|
+
dsh_home="${DSH_HOME:-$HOME/.dsh}"
|
|
17
|
+
export SESSIONS_ROOT="${SESSIONS_ROOT:-$dsh_home/sessions}"
|
|
18
|
+
# Resolve the script's real location first: when invoked through a symlinked
|
|
19
|
+
# skill path (e.g. ~/.agents/skills/dsh-safe-restart/...), BASH_SOURCE keeps the
|
|
20
|
+
# link and "../../.." lands in ~/.agents — breaking the session-health preflight
|
|
21
|
+
# import (lib/session-health.js). readlink -f recovers the package checkout.
|
|
22
|
+
SCRIPT_SRC="${BASH_SOURCE[0]}"
|
|
23
|
+
if resolved="$(readlink -f "$SCRIPT_SRC" 2>/dev/null)" && [ -n "$resolved" ]; then
|
|
24
|
+
SCRIPT_SRC="$resolved"
|
|
25
|
+
fi
|
|
26
|
+
PLUGIN_DIR="${DSH_SUPERVISOR_PLUGIN_DIR:-$(cd "$(dirname "$SCRIPT_SRC")/../../.." && pwd)}"
|
|
27
|
+
confirmed=false
|
|
28
|
+
dry_run=false
|
|
29
|
+
auto_mode=false
|
|
30
|
+
check_supervisor=false
|
|
31
|
+
reload_supervisor=false
|
|
32
|
+
|
|
33
|
+
dry_boot_and_verify() {
|
|
34
|
+
local dsh_repo="$1"
|
|
35
|
+
local port="${2:-0}"
|
|
36
|
+
local marker="${3:-}"
|
|
37
|
+
local dsh_home
|
|
38
|
+
dsh_home="$(mktemp -d)"
|
|
39
|
+
local log_tmp
|
|
40
|
+
log_tmp="$(mktemp)"
|
|
41
|
+
# ephemeral boot with isolated DSH_HOME
|
|
42
|
+
DSH_HOME="$dsh_home" pnpm --dir "$dsh_repo" exec dsh web --port "$port" --no-open >"$log_tmp" 2>&1 &
|
|
43
|
+
local pid=$!
|
|
44
|
+
local ok=false
|
|
45
|
+
for _ in $(seq 1 15); do
|
|
46
|
+
if curl -s "http://127.0.0.1:$port/" 2>/dev/null | grep -q "${marker:-}"; then
|
|
47
|
+
ok=true
|
|
48
|
+
break
|
|
49
|
+
fi
|
|
50
|
+
if ! kill -0 "$pid" 2>/dev/null; then break; fi
|
|
51
|
+
sleep 1
|
|
52
|
+
done
|
|
53
|
+
kill -TERM "$pid" 2>/dev/null || true
|
|
54
|
+
wait "$pid" 2>/dev/null || true
|
|
55
|
+
rm -rf "$dsh_home" "$log_tmp"
|
|
56
|
+
[[ "$ok" == true ]]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
usage() {
|
|
60
|
+
cat <<'EOF'
|
|
61
|
+
Usage: restart-dsh-web.sh --repo <deepseek-harness> [--log <path>] [--confirm|--auto] [--dry-run]
|
|
62
|
+
restart-dsh-web.sh --check-supervisor
|
|
63
|
+
restart-dsh-web.sh --reload-supervisor
|
|
64
|
+
|
|
65
|
+
Safely hand over the DSH Web process that owns ports 3000 and 3080.
|
|
66
|
+
|
|
67
|
+
Options:
|
|
68
|
+
--repo <path> DeepSeek Harness checkout (or set DSH_REPO).
|
|
69
|
+
--log <path> Append-only launch log (or set DSH_RESTART_LOG).
|
|
70
|
+
--confirm Permit a real process handover (human-gated). Required unless --dry-run.
|
|
71
|
+
--auto Permit auto handover (supervisor, no consent prompt). Alias for --confirm with auto log prefix.
|
|
72
|
+
--dry-run Print the resolved process tree; never stop or launch anything.
|
|
73
|
+
--check-supervisor Report supervisor-daemon freshness (fresh|stale|absent); changes nothing.
|
|
74
|
+
--reload-supervisor Reload the daemon only when it predates the newest lib/*.js build.
|
|
75
|
+
-h, --help Show this help text.
|
|
76
|
+
|
|
77
|
+
The supervisor daemon caches this package's lib/*.js in RAM like `dsh web`
|
|
78
|
+
does, and a stale daemon rolls back a healthy boot with its old rules. Every
|
|
79
|
+
real restart therefore reloads it first when stale; DSH_SUPERVISOR_RELOAD_WAIT
|
|
80
|
+
(seconds, default 15) bounds the wait for systemd to bring it back.
|
|
81
|
+
EOF
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fail() {
|
|
85
|
+
printf '[restart] FAIL: %s\n' "$1" >&2
|
|
86
|
+
exit "${2:-1}"
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
# --- supervisor daemon freshness -------------------------------------------------
|
|
90
|
+
# `dsh web` is not the only process that caches this package's lib/*.js in RAM:
|
|
91
|
+
# the standalone dsh-web-supervisor daemon does too, and it is the process that
|
|
92
|
+
# decides whether a boot is healthy or must be rolled back. A daemon started
|
|
93
|
+
# BEFORE the newest build keeps running the old rollback rules — on 2026-09-13
|
|
94
|
+
# exactly that judged a slow boot as down and rolled `dsh web` back in a loop
|
|
95
|
+
# ("rollback - degraded: This operation was aborted") while the port answered in
|
|
96
|
+
# 1.4ms. So: never swap `dsh web` under a stale daemon.
|
|
97
|
+
#
|
|
98
|
+
# Only systemd owns a relaunch (dsh-web-supervisor.service has Restart=always);
|
|
99
|
+
# a daemon someone started by hand has no owner to bring it back, so this step
|
|
100
|
+
# reports and leaves it alone rather than killing it.
|
|
101
|
+
|
|
102
|
+
# MainPID of the supervisor unit, or empty unless that pid really is the daemon.
|
|
103
|
+
supervisor_main_pid() {
|
|
104
|
+
local pid cmdline
|
|
105
|
+
pid="$(systemctl --user show -p MainPID --value dsh-web-supervisor.service 2>/dev/null | tr -dc '0-9' || true)"
|
|
106
|
+
[[ -n "$pid" && "$pid" != 0 ]] || return 0
|
|
107
|
+
[[ -d "/proc/$pid" ]] || return 0
|
|
108
|
+
# Never signal a pid that is not this daemon, whatever systemd reports.
|
|
109
|
+
cmdline="$(tr '\0' ' ' <"/proc/$pid/cmdline" 2>/dev/null || true)"
|
|
110
|
+
[[ "$cmdline" == *"bin.js daemon"* ]] || return 0
|
|
111
|
+
printf '%s' "$pid"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# Newest mtime among the package's built lib/*.js, or empty when unbuilt.
|
|
115
|
+
newest_lib_mtime() {
|
|
116
|
+
local newest="" file stamp
|
|
117
|
+
for file in "$PLUGIN_DIR"/lib/*.js; do
|
|
118
|
+
[[ -f "$file" ]] || continue
|
|
119
|
+
stamp="$(stat -c %Y "$file" 2>/dev/null || true)"
|
|
120
|
+
[[ -n "$stamp" ]] || continue
|
|
121
|
+
if [[ -z "$newest" || "$stamp" -gt "$newest" ]]; then newest="$stamp"; fi
|
|
122
|
+
done
|
|
123
|
+
printf '%s' "$newest"
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
# Classify the daemon: fresh | stale | absent | unknown.
|
|
127
|
+
supervisor_daemon_state() {
|
|
128
|
+
SUPERVISOR_PID="$(supervisor_main_pid)"
|
|
129
|
+
SUPERVISOR_LIB="$(newest_lib_mtime)"
|
|
130
|
+
if [[ -z "$SUPERVISOR_PID" ]]; then
|
|
131
|
+
SUPERVISOR_VERDICT=absent
|
|
132
|
+
return 0
|
|
133
|
+
fi
|
|
134
|
+
SUPERVISOR_START="$(stat -c %Y "/proc/$SUPERVISOR_PID" 2>/dev/null || true)"
|
|
135
|
+
if [[ -z "$SUPERVISOR_START" || -z "$SUPERVISOR_LIB" ]]; then
|
|
136
|
+
SUPERVISOR_VERDICT=unknown
|
|
137
|
+
return 0
|
|
138
|
+
fi
|
|
139
|
+
if (( SUPERVISOR_LIB > SUPERVISOR_START )); then
|
|
140
|
+
SUPERVISOR_VERDICT=stale
|
|
141
|
+
else
|
|
142
|
+
SUPERVISOR_VERDICT=fresh
|
|
143
|
+
fi
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
print_supervisor_state() {
|
|
147
|
+
case "$SUPERVISOR_VERDICT" in
|
|
148
|
+
fresh) printf '[restart] supervisor daemon: fresh pid=%s start=%s lib=%s\n' "$SUPERVISOR_PID" "$SUPERVISOR_START" "$SUPERVISOR_LIB" ;;
|
|
149
|
+
stale) printf '[restart] supervisor daemon: stale pid=%s start=%s lib=%s (built after this daemon started)\n' "$SUPERVISOR_PID" "$SUPERVISOR_START" "$SUPERVISOR_LIB" ;;
|
|
150
|
+
unknown) printf '[restart] supervisor daemon: unknown pid=%s (lib build time unavailable)\n' "$SUPERVISOR_PID" ;;
|
|
151
|
+
*) printf '[restart] supervisor daemon: absent\n' ;;
|
|
152
|
+
esac
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
# Reload the daemon when it predates the newest build; report the verdict either
|
|
156
|
+
# way. A failed reload is fatal: continuing would hand the swap to the very
|
|
157
|
+
# process that can roll it back in a loop.
|
|
158
|
+
ensure_supervisor_current() {
|
|
159
|
+
supervisor_daemon_state
|
|
160
|
+
if [[ "$SUPERVISOR_VERDICT" != stale ]]; then
|
|
161
|
+
print_supervisor_state
|
|
162
|
+
return 0
|
|
163
|
+
fi
|
|
164
|
+
local old_pid="$SUPERVISOR_PID"
|
|
165
|
+
local wait_s="${DSH_SUPERVISOR_RELOAD_WAIT:-15}"
|
|
166
|
+
local new_pid=""
|
|
167
|
+
print_supervisor_state
|
|
168
|
+
printf '[restart] reloading the supervisor daemon so it runs the built lib/*.js\n'
|
|
169
|
+
kill -TERM "$old_pid" 2>/dev/null || true
|
|
170
|
+
for _ in $(seq 1 "$wait_s"); do
|
|
171
|
+
sleep 1
|
|
172
|
+
new_pid="$(supervisor_main_pid)"
|
|
173
|
+
if [[ -n "$new_pid" && "$new_pid" != "$old_pid" ]]; then break; fi
|
|
174
|
+
new_pid=""
|
|
175
|
+
done
|
|
176
|
+
if [[ -z "$new_pid" ]]; then
|
|
177
|
+
printf '[restart] FAIL: supervisor daemon %s did not come back within %ss\n' "$old_pid" "$wait_s" >&2
|
|
178
|
+
return 1
|
|
179
|
+
fi
|
|
180
|
+
printf '[restart] supervisor daemon: reloaded %s -> %s\n' "$old_pid" "$new_pid"
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
# Single-flight + log scoping (D6, spec 2026-09-13-supervisor-safety-net-design):
|
|
184
|
+
# take the SAME boot.lock and append the SAME boot-boundary sentinel as the
|
|
185
|
+
# supervisor's own performSingleBootRestart, by calling the package's helper —
|
|
186
|
+
# never by re-implementing either marker here. The lock is owned by this shell's
|
|
187
|
+
# PID ($$), so it stays valid for as long as the script runs, and it is released
|
|
188
|
+
# on every exit path via the trap below.
|
|
189
|
+
BOOT_GUARD_HELPER="${PLUGIN_DIR}/lib/bin.js"
|
|
190
|
+
boot_guard_acquire() {
|
|
191
|
+
if [[ ! -f "$BOOT_GUARD_HELPER" ]]; then
|
|
192
|
+
fail "boot.lock helper is missing: $BOOT_GUARD_HELPER (run 'pnpm build' in the supervisor package)" 69
|
|
193
|
+
fi
|
|
194
|
+
if ! node "$BOOT_GUARD_HELPER" boot-guard acquire --pid "$$" >>"$log" 2>&1; then
|
|
195
|
+
fail 'another boot holds boot.lock (a dsh web boot or another restart is in flight)' 75
|
|
196
|
+
fi
|
|
197
|
+
}
|
|
198
|
+
boot_guard_release() {
|
|
199
|
+
[[ -f "$BOOT_GUARD_HELPER" ]] || return 0
|
|
200
|
+
node "$BOOT_GUARD_HELPER" boot-guard release --pid "$$" >>"$log" 2>&1 || true
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
while (($#)); do
|
|
204
|
+
case "$1" in
|
|
205
|
+
--repo)
|
|
206
|
+
(($# >= 2)) || fail '--repo requires a path' 64
|
|
207
|
+
repo="$2"
|
|
208
|
+
shift 2
|
|
209
|
+
;;
|
|
210
|
+
--log)
|
|
211
|
+
(($# >= 2)) || fail '--log requires a path' 64
|
|
212
|
+
log="$2"
|
|
213
|
+
shift 2
|
|
214
|
+
;;
|
|
215
|
+
--confirm)
|
|
216
|
+
confirmed=true
|
|
217
|
+
shift
|
|
218
|
+
;;
|
|
219
|
+
--auto)
|
|
220
|
+
confirmed=true
|
|
221
|
+
auto_mode=true
|
|
222
|
+
shift
|
|
223
|
+
;;
|
|
224
|
+
--dry-run)
|
|
225
|
+
dry_run=true
|
|
226
|
+
shift
|
|
227
|
+
;;
|
|
228
|
+
--check-supervisor)
|
|
229
|
+
check_supervisor=true
|
|
230
|
+
shift
|
|
231
|
+
;;
|
|
232
|
+
--reload-supervisor)
|
|
233
|
+
reload_supervisor=true
|
|
234
|
+
shift
|
|
235
|
+
;;
|
|
236
|
+
-h|--help)
|
|
237
|
+
usage
|
|
238
|
+
exit 0
|
|
239
|
+
;;
|
|
240
|
+
*)
|
|
241
|
+
fail "unknown option: $1" 64
|
|
242
|
+
;;
|
|
243
|
+
esac
|
|
244
|
+
done
|
|
245
|
+
|
|
246
|
+
# Standalone supervisor-daemon modes: they never touch dsh web, so they need no
|
|
247
|
+
# --repo, no listeners and no consent.
|
|
248
|
+
if [[ "$check_supervisor" == true ]]; then
|
|
249
|
+
supervisor_daemon_state
|
|
250
|
+
print_supervisor_state
|
|
251
|
+
exit 0
|
|
252
|
+
fi
|
|
253
|
+
if [[ "$reload_supervisor" == true ]]; then
|
|
254
|
+
ensure_supervisor_current || fail 'supervisor daemon could not be reloaded — fix it before swapping dsh web' 70
|
|
255
|
+
exit 0
|
|
256
|
+
fi
|
|
257
|
+
|
|
258
|
+
[[ -n "$repo" ]] || fail 'provide --repo or DSH_REPO' 64
|
|
259
|
+
[[ -f "$repo/package.json" ]] || fail "repo has no package.json: $repo" 64
|
|
260
|
+
if [[ "$dry_run" != true && "$confirmed" != true ]]; then
|
|
261
|
+
fail 'refusing live restart without --confirm' 64
|
|
262
|
+
fi
|
|
263
|
+
|
|
264
|
+
for command in ss ps grep sort; do
|
|
265
|
+
command -v "$command" >/dev/null 2>&1 || fail "required command is unavailable: $command"
|
|
266
|
+
done
|
|
267
|
+
|
|
268
|
+
listener_pids="$({ ss -tlnp 2>/dev/null || true; } | grep -E ':(3000|3080|3081|3082)([[:space:]]|$)' | grep -oE 'pid=[0-9]+' | cut -d= -f2 | sort -u || true)"
|
|
269
|
+
[[ -n "$listener_pids" ]] || fail 'no listeners found on ports 3000/3080 (proxy) or 3081/3082 (local-pin-gate topology)'
|
|
270
|
+
|
|
271
|
+
resolve_tree() {
|
|
272
|
+
local current="$1"
|
|
273
|
+
local parent command_line
|
|
274
|
+
while [[ -n "$current" && "$current" != 1 ]]; do
|
|
275
|
+
command_line="$(ps -o cmd= -p "$current" 2>/dev/null || true)"
|
|
276
|
+
# Never walk into a service manager: when dsh-web runs as a systemd
|
|
277
|
+
# --user unit (ExecStart=node ... directly, no intervening pnpm
|
|
278
|
+
# wrapper), the parent of the listener process IS the manager itself.
|
|
279
|
+
# Without this guard the loop keeps climbing (no "pnpm" match, parent
|
|
280
|
+
# != 1 yet) and includes the manager's own PID in tree_pids -- SIGTERM
|
|
281
|
+
# to it tears down every user unit, not just dsh-web (2026-08-28
|
|
282
|
+
# incident: killed the whole systemd --user session).
|
|
283
|
+
case "$command_line" in
|
|
284
|
+
*systemd\ --user*) break ;;
|
|
285
|
+
esac
|
|
286
|
+
printf '%s\n' "$current"
|
|
287
|
+
[[ "$command_line" == *pnpm* ]] && break
|
|
288
|
+
parent="$(ps -o ppid= -p "$current" 2>/dev/null | tr -d '[:space:]')"
|
|
289
|
+
[[ "$parent" == "$current" ]] && break
|
|
290
|
+
current="$parent"
|
|
291
|
+
done
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
tree_pids="$(for pid in $listener_pids; do resolve_tree "$pid"; done | sort -u)"
|
|
295
|
+
[[ -n "$tree_pids" ]] || fail 'could not resolve a process tree for the listeners'
|
|
296
|
+
|
|
297
|
+
# dsh-web.service has Restart=always: a raw `kill -TERM` on its MainPID looks
|
|
298
|
+
# like a crash to systemd, which immediately relaunches it -- racing this
|
|
299
|
+
# script's own relaunch for ports 3000/3080 (observed live 2026-08-28: restart
|
|
300
|
+
# counter climbed past 20 before either side won). When systemd owns it,
|
|
301
|
+
# defer the whole stop/start to systemctl instead of managing PIDs directly.
|
|
302
|
+
systemd_managed=false
|
|
303
|
+
systemctl --user is-active --quiet dsh-web.service 2>/dev/null && systemd_managed=true
|
|
304
|
+
|
|
305
|
+
printf '[restart] listener pids: %s\n' "$(tr '\n' ' ' <<<"$listener_pids")"
|
|
306
|
+
printf '[restart] process tree: %s\n' "$(tr '\n' ' ' <<<"$tree_pids")"
|
|
307
|
+
printf '[restart] managed by: %s\n' "$([[ "$systemd_managed" == true ]] && echo 'systemd (dsh-web.service)' || echo 'raw process (no systemd unit)')"
|
|
308
|
+
|
|
309
|
+
if [[ "$dry_run" == true ]]; then
|
|
310
|
+
printf '[restart] dry-run: no process will be stopped or launched\n'
|
|
311
|
+
exit 0
|
|
312
|
+
fi
|
|
313
|
+
|
|
314
|
+
# --- safe-guard: don't restart while tools are still running (torn prevention) ---
|
|
315
|
+
# Scan for dangling open turns (turn/start without turn/end) within last 5m.
|
|
316
|
+
# If found, wait up to 30s for them to finish, then require --auto to force.
|
|
317
|
+
check_dangling() {
|
|
318
|
+
local count_dangling
|
|
319
|
+
count_dangling() {
|
|
320
|
+
node --input-type=module <<'NODE' 2>/dev/null || echo 0
|
|
321
|
+
import fs from 'node:fs'
|
|
322
|
+
import { execSync } from 'node:child_process'
|
|
323
|
+
try{
|
|
324
|
+
const root = (process.env.DSH_HOME || (await import('node:os')).homedir() + '/.dsh') + '/sessions'
|
|
325
|
+
let count=0
|
|
326
|
+
for(const proj of fs.readdirSync(root)){
|
|
327
|
+
const pp = root + '/' + proj
|
|
328
|
+
try{ if(!fs.statSync(pp).isDirectory()) continue }catch{continue}
|
|
329
|
+
for(const sess of fs.readdirSync(pp)){
|
|
330
|
+
const p = pp + '/' + sess + '/session.jsonl.zstd'
|
|
331
|
+
try{ fs.statSync(p) }catch{continue}
|
|
332
|
+
try{
|
|
333
|
+
const st = fs.statSync(p)
|
|
334
|
+
if(Date.now() - st.mtimeMs > 5*60*1000) continue
|
|
335
|
+
}catch{continue}
|
|
336
|
+
try{
|
|
337
|
+
const out = execSync(`zstd -d -c ${JSON.stringify(p)} 2>/dev/null | tail -n 20`, {encoding:'utf8', timeout:2000})
|
|
338
|
+
const lastStart = out.lastIndexOf('"type":"turn/start"')
|
|
339
|
+
if(lastStart!==-1 && !out.slice(lastStart).includes('"type":"turn/end"')) count++
|
|
340
|
+
}catch{}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
console.log(count)
|
|
344
|
+
}catch{ console.log(0) }
|
|
345
|
+
NODE
|
|
346
|
+
}
|
|
347
|
+
local dangling
|
|
348
|
+
dangling="$(count_dangling)"
|
|
349
|
+
if [[ "$dangling" != "0" && -n "$dangling" ]]; then
|
|
350
|
+
printf '[restart] WARN: %s dangling open turn(s) within 5m — tools may be running\n' "$dangling" | tee -a "$log" >&2
|
|
351
|
+
if [[ "$auto_mode" != true ]]; then
|
|
352
|
+
printf '[restart] waiting 30s for tools to finish (re-run with --auto to force)\n' | tee -a "$log" >&2
|
|
353
|
+
for _ in $(seq 1 30); do sleep 1; done
|
|
354
|
+
local dangling2
|
|
355
|
+
dangling2="$(count_dangling)"
|
|
356
|
+
if [[ "$dangling2" != "0" && -n "$dangling2" ]]; then
|
|
357
|
+
printf '[restart] still %s dangling after wait — aborting (use --auto to force)\n' "$dangling2" | tee -a "$log" >&2
|
|
358
|
+
fail "refusing restart with $dangling2 dangling turn(s) — tools still running" 64
|
|
359
|
+
fi
|
|
360
|
+
fi
|
|
361
|
+
fi
|
|
362
|
+
}
|
|
363
|
+
check_dangling
|
|
364
|
+
|
|
365
|
+
mkdir -p "$(dirname "$log")"
|
|
366
|
+
printf '[restart] stopping process tree: %s\n' "$(tr '\n' ' ' <<<"$tree_pids")" >> "$log"
|
|
367
|
+
|
|
368
|
+
# Mark this as an intentional restart before the port goes down, so
|
|
369
|
+
# dsh-web-supervisor's health poll does not race us with its own rollback.
|
|
370
|
+
# The boot.lock below is the single-flight half of the same contract; both are
|
|
371
|
+
# released on every exit path (success or failure) via the trap.
|
|
372
|
+
mkdir -p "$(dirname "$log")"
|
|
373
|
+
boot_guard_acquire
|
|
374
|
+
mkdir -p "$(dirname "$marker")"
|
|
375
|
+
date -Iseconds > "$marker"
|
|
376
|
+
trap 'boot_guard_release; rm -f "$marker"' EXIT
|
|
377
|
+
|
|
378
|
+
# Reload a stale supervisor daemon BEFORE the swap: it is the process that
|
|
379
|
+
# decides whether the boot below is healthy, and a daemon running pre-build
|
|
380
|
+
# rules can roll it back in a loop. Done while dsh web is still up, so the
|
|
381
|
+
# fresh daemon observes the planned-restart marker written just above.
|
|
382
|
+
ensure_supervisor_current || fail 'stale supervisor daemon could not be reloaded — refusing to swap dsh web under old rollback rules' 70
|
|
383
|
+
|
|
384
|
+
if [[ "$systemd_managed" == true ]]; then
|
|
385
|
+
# systemctl stop is a clean, intentional stop -- Restart=always does not
|
|
386
|
+
# fire for it, unlike an out-of-band kill of the unit's MainPID.
|
|
387
|
+
printf '[restart] stopping dsh-web.service via systemctl\n' >> "$log"
|
|
388
|
+
systemctl --user stop dsh-web.service 2>>"$log" || fail 'systemctl --user stop dsh-web.service failed'
|
|
389
|
+
else
|
|
390
|
+
kill -TERM $tree_pids 2>/dev/null || true
|
|
391
|
+
|
|
392
|
+
tree_stopped=false
|
|
393
|
+
for _ in $(seq 1 20); do
|
|
394
|
+
alive=false
|
|
395
|
+
for pid in $tree_pids; do
|
|
396
|
+
if kill -0 "$pid" 2>/dev/null; then
|
|
397
|
+
alive=true
|
|
398
|
+
break
|
|
399
|
+
fi
|
|
400
|
+
done
|
|
401
|
+
if [[ "$alive" == false ]]; then
|
|
402
|
+
tree_stopped=true
|
|
403
|
+
break
|
|
404
|
+
fi
|
|
405
|
+
sleep 0.5
|
|
406
|
+
done
|
|
407
|
+
|
|
408
|
+
if [[ "$tree_stopped" != true ]]; then
|
|
409
|
+
printf '[restart] process tree did not stop after TERM; sending KILL\n' >> "$log"
|
|
410
|
+
kill -KILL $tree_pids 2>/dev/null || true
|
|
411
|
+
sleep 1
|
|
412
|
+
fi
|
|
413
|
+
fi
|
|
414
|
+
|
|
415
|
+
for port in 3000 3080 3081 3082; do
|
|
416
|
+
if ss -tln 2>/dev/null | grep -q ":$port "; then
|
|
417
|
+
fail "port $port remains held; refusing to double-boot"
|
|
418
|
+
fi
|
|
419
|
+
done
|
|
420
|
+
|
|
421
|
+
# Pre-flight: never let a corrupt session log brick the boot
|
|
422
|
+
# Runs only AFTER the old tree is stopped and the ports are free (above) and
|
|
423
|
+
# BEFORE the fresh boot below, so a live writer can never race the repair.
|
|
424
|
+
# Deliberately NON-FATAL (the block's exit status is dropped): a transient
|
|
425
|
+
# scan error must never block a legitimate restart.
|
|
426
|
+
if [ -n "$SESSIONS_ROOT" ] && [ -d "$SESSIONS_ROOT" ]; then
|
|
427
|
+
node --input-type=module -e "import('${PLUGIN_DIR}/lib/session-health.js').then(async m => {
|
|
428
|
+
const r = await m.runSessionHealthCheck(process.env.SESSIONS_ROOT, { repair: true, quarantine: false });
|
|
429
|
+
console.log('[session-health] fixed=' + r.fixed + ' quarantined=' + r.quarantined + ' remaining=' + r.remaining);
|
|
430
|
+
})" || true
|
|
431
|
+
fi
|
|
432
|
+
|
|
433
|
+
command -v curl >/dev/null 2>&1 || fail 'required command is unavailable: curl'
|
|
434
|
+
|
|
435
|
+
if [[ "$systemd_managed" == true ]]; then
|
|
436
|
+
printf '[restart] starting dsh-web.service via systemctl\n' >> "$log"
|
|
437
|
+
systemctl --user start dsh-web.service 2>>"$log" || fail 'systemctl --user start dsh-web.service failed'
|
|
438
|
+
else
|
|
439
|
+
command -v setsid >/dev/null 2>&1 || fail 'required command is unavailable: setsid'
|
|
440
|
+
command -v pnpm >/dev/null 2>&1 || fail 'required command is unavailable: pnpm'
|
|
441
|
+
|
|
442
|
+
printf '[restart] launching DSH Web from %s\n' "$repo" >> "$log"
|
|
443
|
+
(
|
|
444
|
+
cd "$repo"
|
|
445
|
+
setsid nohup "$(command -v pnpm)" dsh web --no-open </dev/null >> "$log" 2>&1 &
|
|
446
|
+
)
|
|
447
|
+
fi
|
|
448
|
+
|
|
449
|
+
for _ in $(seq 1 90); do
|
|
450
|
+
# 401 is healthy: dsh-web is up but requires the browser token (matches
|
|
451
|
+
# dsh-web-supervisor's own health-poller convention since DSH 0.1.2).
|
|
452
|
+
# Under the local-pin-gate topology (2026-09-02) :3080 is the maestro PIN
|
|
453
|
+
# proxy (303 -> login / 200 authed) and the raw webserver lives on :3082 —
|
|
454
|
+
# accept any dsh-web surface that answers 200/401/303.
|
|
455
|
+
code_3080="$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:3080/ || true)"
|
|
456
|
+
if [[ "$code_3080" == 200 || "$code_3080" == 401 || "$code_3080" == 303 ]]; then
|
|
457
|
+
printf '[restart] DSH Web is serving HTTP %s on port 3080\n' "$code_3080"
|
|
458
|
+
exit 0
|
|
459
|
+
fi
|
|
460
|
+
code_3082="$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:3082/ || true)"
|
|
461
|
+
if [[ "$code_3082" == 200 || "$code_3082" == 401 ]]; then
|
|
462
|
+
printf '[restart] DSH Web raw webserver is serving HTTP %s on port 3082\n' "$code_3082"
|
|
463
|
+
exit 0
|
|
464
|
+
fi
|
|
465
|
+
sleep 1
|
|
466
|
+
done
|
|
467
|
+
|
|
468
|
+
fail 'DSH Web did not serve HTTP 200/401/303 on :3080 (PIN proxy) or :3082 (raw webserver) before timeout'
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-maestro-core — client auto-reload for DSH Web after restart.
|
|
3
|
+
* Hybrid: polls `HEAD /` when the server is down (offline/WebSocket close)
|
|
4
|
+
* and reloads as soon as it is back. The host also pushes a reload via
|
|
5
|
+
* `POST /dsh-maestro-supervisor-reload` (loopback) when it recovers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function apply(ctx: any): void {
|
|
9
|
+
try {
|
|
10
|
+
ctx.effect(() => {
|
|
11
|
+
let timer: ReturnType<typeof setInterval> | null = null
|
|
12
|
+
let reloading = false
|
|
13
|
+
|
|
14
|
+
const checkAndReload = async () => {
|
|
15
|
+
if (reloading) return
|
|
16
|
+
try {
|
|
17
|
+
const res = await fetch('/', { method: 'HEAD', cache: 'no-store' })
|
|
18
|
+
if (res.ok) {
|
|
19
|
+
reloading = true
|
|
20
|
+
if (timer) { clearInterval(timer); timer = null }
|
|
21
|
+
window.location.reload()
|
|
22
|
+
}
|
|
23
|
+
} catch {
|
|
24
|
+
// still down — keep polling
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const startPolling = () => {
|
|
29
|
+
if (timer || reloading) return
|
|
30
|
+
timer = setInterval(checkAndReload, 1000)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const stopPolling = () => {
|
|
34
|
+
if (timer) { clearInterval(timer); timer = null }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const onOffline = () => startPolling()
|
|
38
|
+
const onOnline = () => {
|
|
39
|
+
const wasPolling = !!timer
|
|
40
|
+
stopPolling()
|
|
41
|
+
if (wasPolling) void checkAndReload()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// DSH Web uses WebSocket for sessions; a close does NOT necessarily mean server down
|
|
45
|
+
// (normal session end also closes WS). Only start polling if HEAD fetch actually fails.
|
|
46
|
+
const OriginalWebSocket = (window as any).WebSocket
|
|
47
|
+
let wsCloseHandler: (() => void) | null = null
|
|
48
|
+
try {
|
|
49
|
+
if (OriginalWebSocket) {
|
|
50
|
+
const Patched = function (this: any, url: string, protocols?: string | string[]) {
|
|
51
|
+
const ws = protocols ? new OriginalWebSocket(url, protocols) : new OriginalWebSocket(url)
|
|
52
|
+
ws.addEventListener('close', () => {
|
|
53
|
+
try {
|
|
54
|
+
const u = new URL(url, window.location.href)
|
|
55
|
+
if (u.host !== window.location.host) return
|
|
56
|
+
} catch { /* ignore */ }
|
|
57
|
+
// Verify server is actually down before polling — avoid reload on normal WS close
|
|
58
|
+
fetch('/', { method: 'HEAD', cache: 'no-store' }).then(res => {
|
|
59
|
+
if (!res.ok) startPolling()
|
|
60
|
+
}).catch(() => startPolling())
|
|
61
|
+
})
|
|
62
|
+
ws.addEventListener('open', () => {
|
|
63
|
+
// If we were polling and WS reopens, check immediately
|
|
64
|
+
if (timer) void checkAndReload()
|
|
65
|
+
})
|
|
66
|
+
return ws
|
|
67
|
+
} as any
|
|
68
|
+
Patched.prototype = OriginalWebSocket.prototype
|
|
69
|
+
Object.setPrototypeOf(Patched, OriginalWebSocket)
|
|
70
|
+
;(window as any).WebSocket = Patched
|
|
71
|
+
wsCloseHandler = () => { (window as any).WebSocket = OriginalWebSocket }
|
|
72
|
+
}
|
|
73
|
+
} catch {}
|
|
74
|
+
|
|
75
|
+
window.addEventListener('offline', onOffline)
|
|
76
|
+
window.addEventListener('online', onOnline)
|
|
77
|
+
document.addEventListener('visibilitychange', () => {
|
|
78
|
+
if (document.visibilityState === 'visible' && timer) void checkAndReload()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
// Host push: POST /dsh-maestro-supervisor-reload/reload (loopback) -> reload
|
|
82
|
+
// The host plugin registers this RPC; the client also listens via fetch polling,
|
|
83
|
+
// but a direct push is instant and avoids the 1s poll delay.
|
|
84
|
+
// We poll for the push by listening to a custom event dispatched from the host's
|
|
85
|
+
// `fetch` response — the host's `notify` after `restartWeb` will trigger it.
|
|
86
|
+
const onHostPush = () => {
|
|
87
|
+
if (!reloading) window.location.reload()
|
|
88
|
+
}
|
|
89
|
+
window.addEventListener('dsh-maestro-supervisor-reload', onHostPush as any)
|
|
90
|
+
|
|
91
|
+
// If the page loads while the server is already down (e.g. hard refresh
|
|
92
|
+
// during restart), start polling immediately.
|
|
93
|
+
void fetch('/', { method: 'HEAD', cache: 'no-store' }).catch(() => startPolling())
|
|
94
|
+
|
|
95
|
+
return () => {
|
|
96
|
+
window.removeEventListener('offline', onOffline)
|
|
97
|
+
window.removeEventListener('online', onOnline)
|
|
98
|
+
window.removeEventListener('dsh-maestro-supervisor-reload', onHostPush as any)
|
|
99
|
+
if (timer) clearInterval(timer)
|
|
100
|
+
if (wsCloseHandler) wsCloseHandler()
|
|
101
|
+
}
|
|
102
|
+
}, 'supervisor:auto-reload')
|
|
103
|
+
} catch {}
|
|
104
|
+
}
|