@ddtcorex/dsh-maestro-core 0.12.1 → 0.14.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/README.md +5 -5
- package/lib/boot-lock.d.ts +1 -1
- package/lib/boot-lock.js +1 -1
- package/lib/cli.d.ts +0 -30
- package/lib/cli.js +2 -202
- package/lib/daemon-freshness.d.ts +4 -4
- package/lib/daemon-freshness.js +4 -4
- package/lib/debug-agent.d.ts +7 -0
- package/lib/debug-agent.js +13 -4
- package/lib/dsh-launch.d.ts +26 -0
- package/lib/dsh-launch.js +27 -0
- package/lib/health-poller.js +3 -3
- package/lib/notifier.js +1 -1
- package/lib/report.js +0 -2
- package/lib/restart-exec.d.ts +1 -1
- package/lib/restart-exec.js +1 -1
- package/lib/restart-guards.d.ts +0 -1
- package/lib/restart-guards.js +1 -11
- package/lib/restart-tool.d.ts +10 -26
- package/lib/restart-tool.js +19 -89
- package/lib/restart-web.d.ts +4 -1
- package/lib/restart-web.js +8 -4
- package/lib/supervisor.d.ts +2 -27
- package/lib/supervisor.js +32 -71
- package/package.json +1 -1
- package/skills/dsh-safe-restart/SKILL.md +8 -7
- package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +20 -36
- package/src/host/boot-lock.ts +1 -1
- package/src/host/cli.ts +2 -172
- package/src/host/daemon-freshness.ts +4 -4
- package/src/host/debug-agent.ts +15 -7
- package/src/host/dsh-launch.ts +48 -0
- package/src/host/health-poller.ts +3 -3
- package/src/host/notifier.ts +1 -1
- package/src/host/report.ts +0 -2
- package/src/host/restart-exec.ts +1 -1
- package/src/host/restart-guards.ts +1 -12
- package/src/host/restart-tool.ts +20 -79
- package/src/host/restart-web.ts +9 -7
- package/src/host/supervisor.ts +38 -84
- package/systemd/dsh-web-supervisor.service.template +2 -0
- package/systemd/dsh-web.service.template +8 -2
- package/lib/snapshot.d.ts +0 -60
- package/lib/snapshot.js +0 -359
- package/src/host/snapshot.ts +0 -338
package/lib/restart-tool.js
CHANGED
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
* dsh web. Scheduling a restart through this tool instead of a raw kill keeps
|
|
4
4
|
* the restart inside the supervisor's ownership loop:
|
|
5
5
|
*
|
|
6
|
-
* 1. dry-boot gate —
|
|
7
|
-
*
|
|
8
|
-
* schedule when that boot serves HTTP.
|
|
6
|
+
* 1. dry-boot gate — boot a copy of the live profile on an ephemeral
|
|
7
|
+
* DSH_HOME first and only schedule when that boot serves HTTP.
|
|
9
8
|
* 2. intent sidecar — record the caller session + reason under
|
|
10
9
|
* ~/.dsh/.supervisor/intents/ for attribution.
|
|
11
10
|
* 3. hand-off — write the restart-request marker (planned-restart.json with
|
|
@@ -13,13 +12,14 @@
|
|
|
13
12
|
* (out-of-band). This tool NEVER restarts the host in-tree.
|
|
14
13
|
*/
|
|
15
14
|
import { join, dirname, resolve } from 'node:path';
|
|
16
|
-
import { mkdtempSync, rmSync, cpSync, existsSync, readFileSync, readdirSync,
|
|
15
|
+
import { mkdtempSync, rmSync, cpSync, existsSync, readFileSync, readdirSync, lstatSync, readlinkSync, symlinkSync, unlinkSync } from 'node:fs';
|
|
17
16
|
import { tmpdir, homedir } from 'node:os';
|
|
18
17
|
import { spawn, execFileSync } from 'node:child_process';
|
|
19
18
|
import { createRequire } from 'node:module';
|
|
20
19
|
import { writeRestartRequest, readRestartRequest } from './restart-guards.js';
|
|
21
20
|
import { intentPath, readIntent, readRestartOutcome } from './intents.js';
|
|
22
21
|
import { supervisorDaemonState, describeDaemonState } from './daemon-freshness.js';
|
|
22
|
+
import { resolveDshLaunch, isDshWebCommand } from './dsh-launch.js';
|
|
23
23
|
/**
|
|
24
24
|
* Copy a live profile tree for an isolated dry-boot. A naive recursive copy
|
|
25
25
|
* breaks `link:` installs: their node_modules entries are relative symlinks
|
|
@@ -105,7 +105,7 @@ export function listDryBootCandidates(readers = {}) {
|
|
|
105
105
|
for (const p of readProc()) {
|
|
106
106
|
if (p.pid === selfPid)
|
|
107
107
|
continue;
|
|
108
|
-
if (
|
|
108
|
+
if (!isDshWebCommand(p.cmd))
|
|
109
109
|
continue;
|
|
110
110
|
const home = /^DSH_HOME=([^\0]*)/m.exec(p.env)?.[1] ?? '';
|
|
111
111
|
if (!home.startsWith(join(tmpdir(), 'dsh-dryboot-')))
|
|
@@ -124,7 +124,9 @@ export function listDryBootCandidates(readers = {}) {
|
|
|
124
124
|
* Boot a copy of the live web profile on an isolated DSH_HOME and verify the
|
|
125
125
|
* plugin tree loads and serves. Returns ok + a one-line detail for the tool
|
|
126
126
|
* message. The spawned tree is killed (best-effort) and the temp home removed.
|
|
127
|
-
* Unit tests mock this (never spawn a real node boot in tests).
|
|
127
|
+
* Unit tests mock this (never spawn a real node boot in tests). The boot uses
|
|
128
|
+
* `resolveDshLaunch`, so `harnessRoot` (the source checkout) only matters
|
|
129
|
+
* when `DSH_BIN` is unset.
|
|
128
130
|
*
|
|
129
131
|
* NOTE (bin.ts finding): the `web` alias already implies `--profile web`, and
|
|
130
132
|
* the web app's own commander program (no allowUnknownOption) rejects a stray
|
|
@@ -144,8 +146,9 @@ export async function dryBootVerify(harnessRoot, opts = {}) {
|
|
|
144
146
|
copyProfileForDryBoot(liveProfile, join(tmpHome, 'profiles', 'web'));
|
|
145
147
|
const port = String(9000 + Math.floor(Math.random() * 1000));
|
|
146
148
|
const url = `http://127.0.0.1:${port}/`;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
+
const launch = resolveDshLaunch(['web', '--no-open', '--port', port], { sourceDir: harnessRoot });
|
|
150
|
+
child = spawn(launch.command, launch.args, {
|
|
151
|
+
cwd: launch.cwd, env: { ...process.env, DSH_HOME: tmpHome }, stdio: ['ignore', 'pipe', 'pipe'],
|
|
149
152
|
});
|
|
150
153
|
child.stdout?.on('data', (d) => logs.push(d.toString()));
|
|
151
154
|
child.stderr?.on('data', (d) => logs.push(d.toString()));
|
|
@@ -211,86 +214,14 @@ export function dryBootFailureDetail(tail, exitCode) {
|
|
|
211
214
|
return `dry-boot failed (exit ${exitCode})`;
|
|
212
215
|
}
|
|
213
216
|
/**
|
|
214
|
-
* Whether the live plugin tree differs from the
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* 2. cordis patch drift — the live profile's web `cordis.patch.yml` text vs
|
|
219
|
-
* baseline (a patch-only config edit changes the boot-time row wiring
|
|
220
|
-
* without touching the manifest — the manifest check alone misses it);
|
|
221
|
-
* 3. plugin-lib drift — any `@ddtcorex` plugin `lib/` file newer than the
|
|
222
|
-
* snapshot moment. Link-installed plugins resolve to the same workspace
|
|
223
|
-
* files in both live and LKG, so the stored copies cannot be compared
|
|
224
|
-
* byte-wise; the snapshot itself is the meaningful baseline and a rebuilt
|
|
225
|
-
* `lib/` bumps a file past it even when the manifest text is unchanged.
|
|
226
|
-
* writeLKG writes `manifest.json` LAST, so its FILE mtime is the
|
|
227
|
-
* authoritative snapshot moment; the snapshot dir mtime is only a
|
|
228
|
-
* fallback for legacy snapshots without a manifest.
|
|
229
|
-
*
|
|
230
|
-
* `statFile` (default `statSync`) reads the metadata so tests can inject a
|
|
231
|
-
* controlled reader instead of relying on filesystem utimes (which CI runners
|
|
232
|
-
* do not reliably reflect). No LKG baseline, a missing file on either side, or
|
|
233
|
-
* any stat/read error means "changed" — the caller falls back to the dry-boot
|
|
234
|
-
* gate.
|
|
217
|
+
* Whether the live plugin tree differs from the last boot. There is no
|
|
218
|
+
* snapshot baseline anymore, so this always reports "changed" and the caller
|
|
219
|
+
* always runs the dry-boot gate. `pluginChanged: false` still skips the gate
|
|
220
|
+
* explicitly; anything else boots the candidate first.
|
|
235
221
|
*/
|
|
236
|
-
export function isPluginTreeChanged(harnessRoot,
|
|
222
|
+
export function isPluginTreeChanged(harnessRoot, _baselineDir, _opts = {}) {
|
|
237
223
|
void harnessRoot;
|
|
238
|
-
|
|
239
|
-
try {
|
|
240
|
-
const entries = existsSync(lkgDir) ? readdirSync(lkgDir).sort() : [];
|
|
241
|
-
const latest = entries[entries.length - 1];
|
|
242
|
-
if (!latest)
|
|
243
|
-
return true; // no baseline → assume changed
|
|
244
|
-
const lkgHome = join(lkgDir, latest, 'profiles', 'web');
|
|
245
|
-
const live = join(homedir(), '.dsh', 'profiles', 'web');
|
|
246
|
-
const lkgManifest = join(lkgHome, 'package.json');
|
|
247
|
-
const liveManifest = join(live, 'package.json');
|
|
248
|
-
if (!existsSync(lkgManifest) || !existsSync(liveManifest))
|
|
249
|
-
return true;
|
|
250
|
-
if (readFileSync(liveManifest, 'utf8') !== readFileSync(lkgManifest, 'utf8'))
|
|
251
|
-
return true;
|
|
252
|
-
// cordis.patch.yml — compare only when at least one side has it (profiles
|
|
253
|
-
// without a patch are the baseline; a patch appearing on either side alone
|
|
254
|
-
// is drift). The text compare keeps the check cheap and hermetic.
|
|
255
|
-
const lkgPatch = join(lkgHome, 'cordis.patch.yml');
|
|
256
|
-
const livePatch = join(live, 'cordis.patch.yml');
|
|
257
|
-
if (existsSync(lkgPatch) || existsSync(livePatch)) {
|
|
258
|
-
if (!existsSync(lkgPatch) || !existsSync(livePatch))
|
|
259
|
-
return true;
|
|
260
|
-
if (readFileSync(livePatch, 'utf8') !== readFileSync(lkgPatch, 'utf8'))
|
|
261
|
-
return true;
|
|
262
|
-
}
|
|
263
|
-
const snapshotManifest = join(lkgDir, latest, 'manifest.json');
|
|
264
|
-
const baseline = existsSync(snapshotManifest)
|
|
265
|
-
? statFile(snapshotManifest).mtimeMs
|
|
266
|
-
: statFile(join(lkgDir, latest)).mtimeMs;
|
|
267
|
-
const livePlugins = join(live, 'node_modules', '@ddtcorex');
|
|
268
|
-
if (existsSync(livePlugins)) {
|
|
269
|
-
for (const name of readdirSync(livePlugins)) {
|
|
270
|
-
const libDir = join(livePlugins, name, 'lib');
|
|
271
|
-
if (!existsSync(libDir))
|
|
272
|
-
continue;
|
|
273
|
-
if (newestFileMtime(libDir, statFile) > baseline)
|
|
274
|
-
return true;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return false;
|
|
278
|
-
}
|
|
279
|
-
catch {
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
/** Newest mtime under a directory; recursion threads the injected stat reader. */
|
|
284
|
-
function newestFileMtime(dir, statFile) {
|
|
285
|
-
let newest = 0;
|
|
286
|
-
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
287
|
-
const p = join(dir, entry.name);
|
|
288
|
-
if (entry.isDirectory())
|
|
289
|
-
newest = Math.max(newest, newestFileMtime(p, statFile));
|
|
290
|
-
else
|
|
291
|
-
newest = Math.max(newest, statFile(p).mtimeMs);
|
|
292
|
-
}
|
|
293
|
-
return newest;
|
|
224
|
+
return true;
|
|
294
225
|
}
|
|
295
226
|
function currentSessionId(exec, fallback) {
|
|
296
227
|
// dsh-tools dispatch hands a ToolRunContext — the exec itself has no
|
|
@@ -326,7 +257,7 @@ export function registerRestartTool(ctx, deps = {}) {
|
|
|
326
257
|
throw new Error('tools service unavailable');
|
|
327
258
|
dispose = tools.register({
|
|
328
259
|
name: 'dsh_web_restart',
|
|
329
|
-
description: 'Schedule a safe restart of the dsh web host: dry-boots the plugin tree
|
|
260
|
+
description: 'Schedule a safe restart of the dsh web host: dry-boots the plugin tree, records an intent for the calling session, and hands the swap to the supervisor daemon (out-of-band — it never restarts in-tree). Returns immediately; the swap lands ~1-5 min later and kills the calling turn, so call this as the LAST action of the turn and verify with dsh_web_restart_status in the NEXT one (never sleep here). Do not call dsh_web_dryboot first — this tool already dry-boots. The result carries the supervisor daemon verdict: a "stale" line means run its `kill -TERM <pid>` before the swap lands.',
|
|
330
261
|
parameters: {
|
|
331
262
|
type: 'object',
|
|
332
263
|
properties: {
|
|
@@ -358,8 +289,7 @@ export function registerRestartTool(ctx, deps = {}) {
|
|
|
358
289
|
return { ok: false, detail: `restart already in progress (requested by ${by}${why}) — check dsh_web_restart_status instead of scheduling another` };
|
|
359
290
|
}
|
|
360
291
|
const harnessRoot = deps.harnessRoot ?? (await import('./paths.js')).resolveDeepseekHarnessDir();
|
|
361
|
-
const
|
|
362
|
-
const changed = args.pluginChanged === true || (args.pluginChanged !== false && isPluginTreeChanged(harnessRoot, lkgDir));
|
|
292
|
+
const changed = args.pluginChanged === true || (args.pluginChanged !== false && isPluginTreeChanged(harnessRoot));
|
|
363
293
|
if (changed) {
|
|
364
294
|
const gate = await doDryBoot(harnessRoot);
|
|
365
295
|
if (!gate.ok)
|
package/lib/restart-web.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type BootLockDeps } from './boot-lock.js';
|
|
2
|
+
import { type DshLaunchDeps } from './dsh-launch.js';
|
|
2
3
|
/** Boot budget: how long a boot may take before its failures are judged. */
|
|
3
4
|
export declare const DEFAULT_BOOT_GRACE_MS = 180000;
|
|
4
5
|
export interface RestartWebDeps {
|
|
@@ -20,7 +21,9 @@ export interface RestartWebResult {
|
|
|
20
21
|
/**
|
|
21
22
|
* The single implementation of a supervised dsh web restart. Single-flight:
|
|
22
23
|
* boot.lock is held from before the marker is written until the port answers,
|
|
23
|
-
* so a racing tick or a second
|
|
24
|
+
* so a racing tick or a second restart skips instead of producing the second
|
|
24
25
|
* systemd start that turned the incident's log tail toxic (2026-09-13).
|
|
25
26
|
*/
|
|
26
27
|
export declare function performSingleBootRestart(deps?: RestartWebDeps): Promise<RestartWebResult>;
|
|
28
|
+
/** The detached last-resort start, appending to the web log. Exported for tests. */
|
|
29
|
+
export declare function buildNohupCommand(logPath: string, deps?: DshLaunchDeps): string;
|
package/lib/restart-web.js
CHANGED
|
@@ -3,13 +3,13 @@ import * as path from 'node:path';
|
|
|
3
3
|
import { buildKillStalePortsCommand, writePlannedRestart } from './restart-guards.js';
|
|
4
4
|
import { serializedSystemdRestart, shouldUseNohupFallback, systemdUnitExists } from './restart-exec.js';
|
|
5
5
|
import { withBootLock } from './boot-lock.js';
|
|
6
|
-
import {
|
|
6
|
+
import { dshLaunchShell } from './dsh-launch.js';
|
|
7
7
|
/** Boot budget: how long a boot may take before its failures are judged. */
|
|
8
8
|
export const DEFAULT_BOOT_GRACE_MS = 180_000;
|
|
9
9
|
/**
|
|
10
10
|
* The single implementation of a supervised dsh web restart. Single-flight:
|
|
11
11
|
* boot.lock is held from before the marker is written until the port answers,
|
|
12
|
-
* so a racing tick or a second
|
|
12
|
+
* so a racing tick or a second restart skips instead of producing the second
|
|
13
13
|
* systemd start that turned the incident's log tail toxic (2026-09-13).
|
|
14
14
|
*/
|
|
15
15
|
export async function performSingleBootRestart(deps = {}) {
|
|
@@ -50,9 +50,13 @@ export async function performSingleBootRestart(deps = {}) {
|
|
|
50
50
|
return { restarted: false, reason: 'another boot already holds boot.lock' };
|
|
51
51
|
return { restarted: true };
|
|
52
52
|
}
|
|
53
|
+
/** The detached last-resort start, appending to the web log. Exported for tests. */
|
|
54
|
+
export function buildNohupCommand(logPath, deps = {}) {
|
|
55
|
+
const launch = dshLaunchShell(['web', '--no-open'], deps);
|
|
56
|
+
return `setsid nohup bash -c 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; ${launch} >> ${JSON.stringify(logPath)} 2>&1' &`;
|
|
57
|
+
}
|
|
53
58
|
async function defaultNohup() {
|
|
54
59
|
const { execSync } = await import('node:child_process');
|
|
55
|
-
const harnessRoot = resolveDeepseekHarnessDir();
|
|
56
60
|
const logPath = path.join(os.homedir(), '.dsh/dsh-web.log');
|
|
57
|
-
execSync(
|
|
61
|
+
execSync(buildNohupCommand(logPath), { timeout: 5000 });
|
|
58
62
|
}
|
package/lib/supervisor.d.ts
CHANGED
|
@@ -2,25 +2,8 @@ import { type HealthState } from './health-poller.js';
|
|
|
2
2
|
import type { RestartRequest } from './restart-guards.js';
|
|
3
3
|
import { type RestartOutcome } from './intents.js';
|
|
4
4
|
import { type MintCookieOpts } from './dsh-session.js';
|
|
5
|
-
/** Partial-restore report a rollback may hand back (cli.ts wires RestoreResult). */
|
|
6
|
-
export interface RollbackSummary {
|
|
7
|
-
target?: string;
|
|
8
|
-
restored?: number;
|
|
9
|
-
skipped?: Array<{
|
|
10
|
-
path: string;
|
|
11
|
-
reason: string;
|
|
12
|
-
}>;
|
|
13
|
-
}
|
|
14
5
|
export interface SupervisorDeps {
|
|
15
6
|
pollHealth: () => Promise<HealthState>;
|
|
16
|
-
writeLKG: () => Promise<{
|
|
17
|
-
ts: string;
|
|
18
|
-
manifest?: any;
|
|
19
|
-
}>;
|
|
20
|
-
writeFailed: () => Promise<{
|
|
21
|
-
ts: string;
|
|
22
|
-
manifest?: any;
|
|
23
|
-
}>;
|
|
24
7
|
writeReport: (opts: {
|
|
25
8
|
ts: string;
|
|
26
9
|
health: HealthState;
|
|
@@ -28,7 +11,6 @@ export interface SupervisorDeps {
|
|
|
28
11
|
logTail?: string;
|
|
29
12
|
gitDiff?: string;
|
|
30
13
|
}) => Promise<string>;
|
|
31
|
-
rollback: (ts?: string) => Promise<RollbackSummary | void>;
|
|
32
14
|
restartWeb?: () => Promise<void>;
|
|
33
15
|
notify: (msg: string) => Promise<void>;
|
|
34
16
|
intervalMs?: number;
|
|
@@ -76,9 +58,8 @@ export declare function resumeViaRpcWithSession(ids: string[], fetchFn?: (url: s
|
|
|
76
58
|
}>;
|
|
77
59
|
export declare class Supervisor {
|
|
78
60
|
private deps;
|
|
79
|
-
private
|
|
80
|
-
private
|
|
81
|
-
private lastLKGWrite;
|
|
61
|
+
private lastRestart;
|
|
62
|
+
private restarting;
|
|
82
63
|
private lastDegradedNotify;
|
|
83
64
|
private consecutiveDown;
|
|
84
65
|
private consecutiveDegraded;
|
|
@@ -104,12 +85,6 @@ export declare class Supervisor {
|
|
|
104
85
|
private findInterruptedRecent;
|
|
105
86
|
private collectGitDiff;
|
|
106
87
|
private attemptAutoResume;
|
|
107
|
-
/**
|
|
108
|
-
* D4: a restore that could not put every entry back must be surfaced loudly.
|
|
109
|
-
* Without this, a half-restored tree reads exactly like a clean recovery in
|
|
110
|
-
* the log and in the operator's Telegram feed.
|
|
111
|
-
*/
|
|
112
|
-
private reportRollbackResult;
|
|
113
88
|
private handleDebugResult;
|
|
114
89
|
tick(): Promise<void>;
|
|
115
90
|
start(): Promise<void>;
|
package/lib/supervisor.js
CHANGED
|
@@ -62,9 +62,8 @@ export async function resumeViaRpcWithSession(ids, fetchFn = globalThis.fetch, o
|
|
|
62
62
|
}
|
|
63
63
|
export class Supervisor {
|
|
64
64
|
deps;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
lastLKGWrite = 0;
|
|
65
|
+
lastRestart = 0;
|
|
66
|
+
restarting = false;
|
|
68
67
|
lastDegradedNotify = 0;
|
|
69
68
|
consecutiveDown = 0;
|
|
70
69
|
consecutiveDegraded = 0;
|
|
@@ -89,7 +88,7 @@ export class Supervisor {
|
|
|
89
88
|
const grace = await this.getEffectiveBootGraceMs();
|
|
90
89
|
// Marker first, and its TTL is the boot budget — not 30 s. The observed
|
|
91
90
|
// boot took 1m52s: after +30 s every poll was judged as a crash, which is
|
|
92
|
-
// exactly how the 2026-09-13
|
|
91
|
+
// exactly how the 2026-09-13 crash report was produced (D5).
|
|
93
92
|
this.getWritePlannedRestart()(grace);
|
|
94
93
|
if (this.deps.restartWeb) {
|
|
95
94
|
// Injected implementation (cli.ts daemon wiring, tests): it owns the
|
|
@@ -318,17 +317,6 @@ export class Supervisor {
|
|
|
318
317
|
await this.deps.notify(`RESUME FAILED: ${ids.length} interrupted sessions (${ids.slice(0, 3).join(', ')}) — ${e?.message ?? String(e)}`).catch(() => { });
|
|
319
318
|
}
|
|
320
319
|
}
|
|
321
|
-
/**
|
|
322
|
-
* D4: a restore that could not put every entry back must be surfaced loudly.
|
|
323
|
-
* Without this, a half-restored tree reads exactly like a clean recovery in
|
|
324
|
-
* the log and in the operator's Telegram feed.
|
|
325
|
-
*/
|
|
326
|
-
async reportRollbackResult(summary, reportPath) {
|
|
327
|
-
if (!summary || !summary.skipped?.length)
|
|
328
|
-
return;
|
|
329
|
-
const first = summary.skipped.slice(0, 3).map(s => `${s.path} (${s.reason})`).join('; ');
|
|
330
|
-
await this.deps.notify(`ROLLBACK PARTIAL: ${summary.restored ?? 0} restored, ${summary.skipped.length} skipped — ${first} (report: ${reportPath})`).catch(() => { });
|
|
331
|
-
}
|
|
332
320
|
handleDebugResult(reportPath, res) {
|
|
333
321
|
if (res.fixed) {
|
|
334
322
|
void this.deps.notify(`FIXED: debug-agent fixed ${reportPath} — ${res.reason}`).catch(() => { });
|
|
@@ -356,7 +344,7 @@ export class Supervisor {
|
|
|
356
344
|
// grace timer → restartWeb once → notify. The suppression marker and the
|
|
357
345
|
// single-flight latch are held until a post-restart health.up tick clears
|
|
358
346
|
// them, so the in-flight down of our own restart can never be mistaken for
|
|
359
|
-
// a crash and raced with a
|
|
347
|
+
// a crash and raced with a second restart by the crash path.
|
|
360
348
|
const restartReq = this.deps.readRestartRequest ? this.deps.readRestartRequest() : undefined;
|
|
361
349
|
if (restartReq?.callerSessionId && !this.restartRequestHandled) {
|
|
362
350
|
this.restartRequestHandled = true;
|
|
@@ -364,10 +352,10 @@ export class Supervisor {
|
|
|
364
352
|
void (async () => {
|
|
365
353
|
try {
|
|
366
354
|
// Debounce crash handling from the moment the restart is issued —
|
|
367
|
-
// the crash path sets
|
|
355
|
+
// the crash path sets lastRestart the same way. A slow boot must
|
|
368
356
|
// not be classified as a crash even after the suppression marker's
|
|
369
357
|
// own 30s TTL runs out.
|
|
370
|
-
this.
|
|
358
|
+
this.lastRestart = this.deps.getTime ? this.deps.getTime() : Date.now();
|
|
371
359
|
await this.restartWeb();
|
|
372
360
|
// restartWeb() already wrote the marker with the boot budget as its
|
|
373
361
|
// TTL; the marker is cleared on the first healthy poll instead.
|
|
@@ -400,12 +388,12 @@ export class Supervisor {
|
|
|
400
388
|
}, 5000);
|
|
401
389
|
return;
|
|
402
390
|
}
|
|
403
|
-
// DEGRADED: http 200 but log has plugin error → report + notify,
|
|
391
|
+
// DEGRADED: http 200 but log has plugin error → report + notify, restart after consecutive threshold
|
|
404
392
|
if (health.degraded) {
|
|
405
393
|
this.consecutiveDown = 0;
|
|
406
394
|
// D1: a degraded verdict while the current boot is unproven is a boot
|
|
407
395
|
// transient, not a plugin error — the incident's report line
|
|
408
|
-
// "
|
|
396
|
+
// "restart — degraded: This operation was aborted" was produced exactly
|
|
409
397
|
// here, five polls after a slow boot.
|
|
410
398
|
if (health.bootPhase === 'booting') {
|
|
411
399
|
this.consecutiveDegraded = 0;
|
|
@@ -474,48 +462,40 @@ export class Supervisor {
|
|
|
474
462
|
catch { }
|
|
475
463
|
return;
|
|
476
464
|
}
|
|
477
|
-
// consecutive degraded threshold reached → treat as down:
|
|
465
|
+
// consecutive degraded threshold reached → treat as down: report + restart
|
|
478
466
|
this.consecutiveDegraded = 0;
|
|
479
467
|
const degradedError = health.error ?? 'degraded plugin error';
|
|
480
|
-
if (this.
|
|
468
|
+
if (this.restarting)
|
|
481
469
|
return;
|
|
482
470
|
const now2 = this.deps.getTime ? this.deps.getTime() : Date.now();
|
|
483
471
|
const debounceMs2 = this.deps.debounceMs ?? 60000;
|
|
484
|
-
if (now2 - this.
|
|
472
|
+
if (now2 - this.lastRestart < debounceMs2)
|
|
485
473
|
return;
|
|
486
|
-
this.
|
|
487
|
-
this.
|
|
474
|
+
this.restarting = true;
|
|
475
|
+
this.lastRestart = now2;
|
|
488
476
|
try {
|
|
489
|
-
const
|
|
490
|
-
const ts2 = failed?.ts ?? new Date().toISOString().replace(/[:.]/g, '-');
|
|
477
|
+
const ts2 = new Date().toISOString().replace(/[:.]/g, '-');
|
|
491
478
|
const logTail2 = health.logTail ?? '';
|
|
492
479
|
const gitDiff2 = await this.collectGitDiff().catch(() => '');
|
|
493
480
|
const degradedHealth = { up: false, httpCode: health.httpCode, error: `degraded → down: ${degradedError}`, logTail: logTail2, degraded: false };
|
|
494
|
-
const reportPath2 = await this.deps.writeReport({ ts: ts2, health: degradedHealth, action: `
|
|
495
|
-
try {
|
|
496
|
-
const summary = await this.deps.rollback();
|
|
497
|
-
await this.reportRollbackResult(summary, reportPath2);
|
|
498
|
-
}
|
|
499
|
-
catch (e) {
|
|
500
|
-
await this.deps.notify(`rollback failed: ${e?.message ?? String(e)} (report: ${reportPath2})`).catch(() => { });
|
|
501
|
-
}
|
|
481
|
+
const reportPath2 = await this.deps.writeReport({ ts: ts2, health: degradedHealth, action: `restart — degraded: ${degradedError}`, logTail: logTail2, gitDiff: gitDiff2 }).catch(() => '');
|
|
502
482
|
if (this.deps.restartWeb) {
|
|
503
483
|
try {
|
|
504
484
|
await this.deps.restartWeb();
|
|
505
|
-
await this.deps.notify(`restarted dsh-web
|
|
485
|
+
await this.deps.notify(`restarted dsh-web (report: ${reportPath2})`).catch(() => { });
|
|
506
486
|
}
|
|
507
487
|
catch (e) {
|
|
508
488
|
await this.deps.notify(`restart dsh-web failed: ${e?.message ?? String(e)} (report: ${reportPath2})`).catch(() => { });
|
|
509
489
|
}
|
|
510
490
|
}
|
|
511
|
-
await this.deps.notify(`DEGRADED →
|
|
491
|
+
await this.deps.notify(`DEGRADED → restart (report: ${reportPath2}, error: ${degradedError})`).catch(() => { });
|
|
512
492
|
try {
|
|
513
493
|
await this.deps.notify(`🔄 dsh web auto-restart — degraded: ${degradedError} — ${new Date().toISOString()}`).catch(() => { });
|
|
514
494
|
}
|
|
515
495
|
catch { }
|
|
516
496
|
}
|
|
517
497
|
finally {
|
|
518
|
-
this.
|
|
498
|
+
this.restarting = false;
|
|
519
499
|
}
|
|
520
500
|
return;
|
|
521
501
|
}
|
|
@@ -571,21 +551,10 @@ export class Supervisor {
|
|
|
571
551
|
}
|
|
572
552
|
}
|
|
573
553
|
}
|
|
574
|
-
// Throttle LKG writes to at most once per 5 minutes
|
|
575
|
-
const now = this.deps.getTime ? this.deps.getTime() : Date.now();
|
|
576
|
-
if (now - this.lastLKGWrite > 5 * 60 * 1000) {
|
|
577
|
-
try {
|
|
578
|
-
await this.deps.writeLKG();
|
|
579
|
-
this.lastLKGWrite = now;
|
|
580
|
-
}
|
|
581
|
-
catch {
|
|
582
|
-
// ignore snapshot errors
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
554
|
return;
|
|
586
555
|
}
|
|
587
556
|
// Single-owner 30s marker: planned restart in progress — suppress crash
|
|
588
|
-
// handling entirely for 30s (no
|
|
557
|
+
// handling entirely for 30s (no writeReport/restart).
|
|
589
558
|
// Check both the new JSON marker (writePlannedRestart) and the legacy
|
|
590
559
|
// isPlannedRestartActive (dsh-safe-web-update plain file) for compat.
|
|
591
560
|
let suppressedByMarker = false;
|
|
@@ -601,7 +570,7 @@ export class Supervisor {
|
|
|
601
570
|
}
|
|
602
571
|
// Down while an intentional restart (e.g. dsh-safe-web-update) is in
|
|
603
572
|
// flight is expected, not a crash — never race it with our own
|
|
604
|
-
//
|
|
573
|
+
// restartWeb.
|
|
605
574
|
if (this.deps.isPlannedRestartActive) {
|
|
606
575
|
let planned = false;
|
|
607
576
|
try {
|
|
@@ -617,7 +586,7 @@ export class Supervisor {
|
|
|
617
586
|
}
|
|
618
587
|
// Down — require consecutive confirmations before treating as a crash.
|
|
619
588
|
// A lone timed-out poll (e.g. a slow plugin-tree boot) must not trigger
|
|
620
|
-
//
|
|
589
|
+
// restart: that restart produces its own transient errors on
|
|
621
590
|
// the next poll, which would otherwise re-trigger this same path forever.
|
|
622
591
|
// D1/D2: while the boot is unproven only a refused connection may advance
|
|
623
592
|
// the down counter — nothing is listening, so the process is gone. A
|
|
@@ -642,40 +611,32 @@ export class Supervisor {
|
|
|
642
611
|
catch { }
|
|
643
612
|
if (this.consecutiveDown < downThreshold)
|
|
644
613
|
return;
|
|
645
|
-
// Down — check debounce and
|
|
646
|
-
if (this.
|
|
614
|
+
// Down — check debounce and restarting state
|
|
615
|
+
if (this.restarting)
|
|
647
616
|
return;
|
|
648
617
|
const now = this.deps.getTime ? this.deps.getTime() : Date.now();
|
|
649
618
|
const debounceMs = this.deps.debounceMs ?? 60000;
|
|
650
|
-
if (now - this.
|
|
619
|
+
if (now - this.lastRestart < debounceMs)
|
|
651
620
|
return;
|
|
652
|
-
this.
|
|
621
|
+
this.restarting = true;
|
|
653
622
|
this.consecutiveDown = 0;
|
|
654
|
-
this.
|
|
623
|
+
this.lastRestart = now;
|
|
655
624
|
try {
|
|
656
|
-
const
|
|
657
|
-
const ts = failed?.ts ?? new Date().toISOString().replace(/[:.]/g, '-');
|
|
625
|
+
const ts = new Date().toISOString().replace(/[:.]/g, '-');
|
|
658
626
|
const logTail = health.logTail ?? '';
|
|
659
627
|
const gitDiff = await this.collectGitDiff().catch(() => '');
|
|
660
|
-
const reportPath = await this.deps.writeReport({ ts, health, action: `
|
|
661
|
-
|
|
662
|
-
const summary = await this.deps.rollback();
|
|
663
|
-
await this.reportRollbackResult(summary, reportPath);
|
|
664
|
-
}
|
|
665
|
-
catch (e) {
|
|
666
|
-
await this.deps.notify(`rollback failed: ${e?.message ?? String(e)} (report: ${reportPath})`).catch(() => { });
|
|
667
|
-
}
|
|
668
|
-
// Always attempt to (re)start dsh web — survives reboot even when rollback is a no-op
|
|
628
|
+
const reportPath = await this.deps.writeReport({ ts, health, action: `restart — ${health.error ?? 'down'}`, logTail, gitDiff }).catch(() => '');
|
|
629
|
+
// Always attempt to (re)start dsh web — survives reboot even when the previous boot left nothing behind
|
|
669
630
|
if (this.deps.restartWeb) {
|
|
670
631
|
try {
|
|
671
632
|
await this.deps.restartWeb();
|
|
672
|
-
await this.deps.notify(`restarted dsh-web
|
|
633
|
+
await this.deps.notify(`restarted dsh-web (report: ${reportPath})`).catch(() => { });
|
|
673
634
|
}
|
|
674
635
|
catch (e) {
|
|
675
636
|
await this.deps.notify(`restart dsh-web failed: ${e?.message ?? String(e)} (report: ${reportPath})`).catch(() => { });
|
|
676
637
|
}
|
|
677
638
|
}
|
|
678
|
-
await this.deps.notify(`CRASH detected →
|
|
639
|
+
await this.deps.notify(`CRASH detected → restart (report: ${reportPath}, error: ${health.error ?? 'down'})`).catch(() => { });
|
|
679
640
|
const runner = this.getRunDebugAgent();
|
|
680
641
|
const isInjected = !!this.deps.runDebugAgent;
|
|
681
642
|
if (isInjected) {
|
|
@@ -698,7 +659,7 @@ export class Supervisor {
|
|
|
698
659
|
}
|
|
699
660
|
}
|
|
700
661
|
finally {
|
|
701
|
-
this.
|
|
662
|
+
this.restarting = false;
|
|
702
663
|
}
|
|
703
664
|
}
|
|
704
665
|
async start() {
|
package/package.json
CHANGED
|
@@ -53,8 +53,8 @@ validate first and get explicit user consent before a real swap.
|
|
|
53
53
|
Keep the live process and its sessions/settings untouched. If the review
|
|
54
54
|
webhook conflicts on a bound port, exclude that provider for the candidate
|
|
55
55
|
or run a no-server composition check instead.
|
|
56
|
-
4. Verify HTTP 200 and the new marker on the candidate.
|
|
57
|
-
|
|
56
|
+
4. Verify HTTP 200 and the new marker on the candidate. Keep the previous working tree available
|
|
57
|
+
until the real swap has passed post-swap checks.
|
|
58
58
|
5. Check the **second** long-lived process, not only `dsh web`: the standalone
|
|
59
59
|
`dsh-web-supervisor` daemon also caches this package's `lib/*.js` in RAM at
|
|
60
60
|
start, and it is the process that judges the new boot. A daemon older than
|
|
@@ -77,15 +77,16 @@ path from documentation.
|
|
|
77
77
|
|
|
78
78
|
```bash
|
|
79
79
|
# Safe inspection only: resolves the serving tree but changes nothing.
|
|
80
|
-
bash <skill-resource-base>/scripts/restart-dsh-web.sh
|
|
81
|
-
--repo /path/to/deepseek-harness --dry-run
|
|
80
|
+
bash <skill-resource-base>/scripts/restart-dsh-web.sh --dry-run
|
|
82
81
|
```
|
|
83
82
|
|
|
84
83
|
Run the `dsh_web_restart` tool instead of executing the helper — the helper is
|
|
85
84
|
for external agents and humans. In-session agents must never run it from their
|
|
86
85
|
own turn (see "Never do these" below).
|
|
87
86
|
|
|
88
|
-
|
|
87
|
+
A systemd-managed `dsh-web.service` needs no checkout. Without systemd the
|
|
88
|
+
helper launches `DSH_BIN` (the packaged `@deepseek-ai/dsh` bin) and only falls
|
|
89
|
+
back to `--repo` / `DSH_REPO` for a source launch. `--log` or `DSH_RESTART_LOG` changes
|
|
89
90
|
the append-only log destination. The helper refuses a real swap without
|
|
90
91
|
`--confirm`, dynamically resolves listeners rather than trusting saved PIDs,
|
|
91
92
|
and refuses to launch if ports are still occupied. `--dry-run` never runs
|
|
@@ -178,8 +179,8 @@ Do not read the top of an old append-only log as liveness evidence. Instead:
|
|
|
178
179
|
3. Check served bytes contain the **new**, unique marker; or make a fresh
|
|
179
180
|
browser/Playwright probe for the changed UI. Shared third-party markers are
|
|
180
181
|
not sufficient proof.
|
|
181
|
-
4. If a marker or live behaviour is wrong, restore the
|
|
182
|
-
|
|
182
|
+
4. If a marker or live behaviour is wrong, restore the previous working tree
|
|
183
|
+
and relaunch with the same guarded process.
|
|
183
184
|
|
|
184
185
|
## Never do these
|
|
185
186
|
|
|
@@ -31,42 +31,18 @@ auto_mode=false
|
|
|
31
31
|
check_supervisor=false
|
|
32
32
|
reload_supervisor=false
|
|
33
33
|
|
|
34
|
-
dry_boot_and_verify() {
|
|
35
|
-
local dsh_repo="$1"
|
|
36
|
-
local port="${2:-0}"
|
|
37
|
-
local marker="${3:-}"
|
|
38
|
-
local dsh_home
|
|
39
|
-
dsh_home="$(mktemp -d)"
|
|
40
|
-
local log_tmp
|
|
41
|
-
log_tmp="$(mktemp)"
|
|
42
|
-
# ephemeral boot with isolated DSH_HOME
|
|
43
|
-
DSH_HOME="$dsh_home" pnpm --dir "$dsh_repo" exec dsh web --port "$port" --no-open >"$log_tmp" 2>&1 &
|
|
44
|
-
local pid=$!
|
|
45
|
-
local ok=false
|
|
46
|
-
for _ in $(seq 1 15); do
|
|
47
|
-
if curl -s "http://127.0.0.1:$port/" 2>/dev/null | grep -q "${marker:-}"; then
|
|
48
|
-
ok=true
|
|
49
|
-
break
|
|
50
|
-
fi
|
|
51
|
-
if ! kill -0 "$pid" 2>/dev/null; then break; fi
|
|
52
|
-
sleep 1
|
|
53
|
-
done
|
|
54
|
-
kill -TERM "$pid" 2>/dev/null || true
|
|
55
|
-
wait "$pid" 2>/dev/null || true
|
|
56
|
-
rm -rf "$dsh_home" "$log_tmp"
|
|
57
|
-
[[ "$ok" == true ]]
|
|
58
|
-
}
|
|
59
|
-
|
|
60
34
|
usage() {
|
|
61
35
|
cat <<'EOF'
|
|
62
|
-
Usage: restart-dsh-web.sh --repo <deepseek-harness> [--log <path>] [--confirm|--auto] [--dry-run]
|
|
36
|
+
Usage: restart-dsh-web.sh [--repo <deepseek-harness>] [--log <path>] [--confirm|--auto] [--dry-run]
|
|
63
37
|
restart-dsh-web.sh --check-supervisor
|
|
64
38
|
restart-dsh-web.sh --reload-supervisor
|
|
65
39
|
|
|
66
40
|
Safely hand over the DSH Web process that owns ports 3000, 3080, 3081 and 3082.
|
|
67
41
|
|
|
68
42
|
Options:
|
|
69
|
-
--repo <path> DeepSeek Harness checkout (or set DSH_REPO).
|
|
43
|
+
--repo <path> DeepSeek Harness checkout (or set DSH_REPO). Only needed for a
|
|
44
|
+
source launch without systemd; DSH_BIN (the packaged
|
|
45
|
+
runtime's bin) takes precedence.
|
|
70
46
|
--log <path> Append-only launch log (or set DSH_RESTART_LOG).
|
|
71
47
|
--confirm Permit a real process handover (human-gated). Required unless --dry-run.
|
|
72
48
|
--auto Permit auto handover (supervisor, no consent prompt). Alias for --confirm with auto log prefix.
|
|
@@ -256,8 +232,9 @@ if [[ "$reload_supervisor" == true ]]; then
|
|
|
256
232
|
exit 0
|
|
257
233
|
fi
|
|
258
234
|
|
|
259
|
-
[[ -n "$repo" ]]
|
|
260
|
-
[[ -f "$repo/package.json" ]] || fail "repo has no package.json: $repo" 64
|
|
235
|
+
if [[ -n "$repo" ]]; then
|
|
236
|
+
[[ -f "$repo/package.json" ]] || fail "repo has no package.json: $repo" 64
|
|
237
|
+
fi
|
|
261
238
|
if [[ "$dry_run" != true && "$confirmed" != true ]]; then
|
|
262
239
|
fail 'refusing live restart without --confirm' 64
|
|
263
240
|
fi
|
|
@@ -438,13 +415,20 @@ if [[ "$systemd_managed" == true ]]; then
|
|
|
438
415
|
systemctl --user start dsh-web.service 2>>"$log" || fail 'systemctl --user start dsh-web.service failed'
|
|
439
416
|
else
|
|
440
417
|
command -v setsid >/dev/null 2>&1 || fail 'required command is unavailable: setsid'
|
|
441
|
-
command -v pnpm >/dev/null 2>&1 || fail 'required command is unavailable: pnpm'
|
|
442
418
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
setsid nohup "$(command -v
|
|
447
|
-
|
|
419
|
+
if [[ -n "${DSH_BIN:-}" ]]; then
|
|
420
|
+
[[ -f "$DSH_BIN" ]] || fail "DSH_BIN does not exist: $DSH_BIN" 64
|
|
421
|
+
printf '[restart] launching DSH Web from %s\n' "$DSH_BIN" >> "$log"
|
|
422
|
+
setsid nohup "$(command -v node)" "$DSH_BIN" web --no-open </dev/null >> "$log" 2>&1 &
|
|
423
|
+
else
|
|
424
|
+
[[ -n "$repo" ]] || fail 'no systemd unit: provide DSH_BIN, --repo or DSH_REPO' 64
|
|
425
|
+
command -v pnpm >/dev/null 2>&1 || fail 'required command is unavailable: pnpm'
|
|
426
|
+
printf '[restart] launching DSH Web from %s\n' "$repo" >> "$log"
|
|
427
|
+
(
|
|
428
|
+
cd "$repo"
|
|
429
|
+
setsid nohup "$(command -v pnpm)" dsh web --no-open </dev/null >> "$log" 2>&1 &
|
|
430
|
+
)
|
|
431
|
+
fi
|
|
448
432
|
fi
|
|
449
433
|
|
|
450
434
|
for _ in $(seq 1 90); do
|