@ddtcorex/dsh-maestro-core 0.13.0 → 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 +3 -3
- 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/restart-tool.d.ts +3 -1
- package/lib/restart-tool.js +8 -4
- package/lib/restart-web.d.ts +3 -0
- package/lib/restart-web.js +7 -3
- package/package.json +1 -1
- package/skills/dsh-safe-restart/SKILL.md +4 -3
- package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +20 -36
- package/src/host/debug-agent.ts +15 -7
- package/src/host/dsh-launch.ts +48 -0
- package/src/host/restart-tool.ts +8 -4
- package/src/host/restart-web.ts +8 -6
- package/systemd/dsh-web-supervisor.service.template +2 -0
- package/systemd/dsh-web.service.template +8 -2
package/README.md
CHANGED
|
@@ -86,7 +86,7 @@ The package declares `dsh.client` (`platform: web`, `inject: ["@deepseek-ai/dsh-
|
|
|
86
86
|
**Pre-flight (required):** before adding to a live profile's `bundles`, dry-boot must pass:
|
|
87
87
|
|
|
88
88
|
```bash
|
|
89
|
-
DSH_HOME=$(mktemp -d)
|
|
89
|
+
DSH_HOME=$(mktemp -d) "$DSH_BIN" web --port 0 &
|
|
90
90
|
# wait for "dsh web: http://127.0.0.1:<port>" and curl 200, then kill
|
|
91
91
|
# This catches load-time failures (missing lib/index.js, stale build, bad cordis.patch.yml)
|
|
92
92
|
# that no in-code try/catch can catch. See dsh-safe-restart skill.
|
|
@@ -195,7 +195,7 @@ curl -s http://127.0.0.1:3080/plugins/@ddtcorex/dsh-maestro-core/client.js | gre
|
|
|
195
195
|
curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:3080/ # 200
|
|
196
196
|
curl -s http://127.0.0.1:3080/dsh-maestro-supervisor-resume/scan -X POST -H 'content-type: application/json' -d '{"type":"client-request","rpcId":"t","method":"scan","payload":{"withinMs":300000}}' | head -c 200
|
|
197
197
|
node --input-type=module -e "import {findDanglingOpenTurns} from './packages/dsh-maestro-core/lib/resume.js'; console.log(await findDanglingOpenTurns(undefined,{withinMs:5*60*1000}))"
|
|
198
|
-
# Create a real dangling:
|
|
198
|
+
# Create a real dangling: "$DSH_BIN" --profile headless "Run bash synchronously sleep 60" & sleep 4; kill $!; node -e "...findDangling..." # should be 1
|
|
199
199
|
# After restart, it should have turn/end interrupted → continue → turn2
|
|
200
200
|
```
|
|
201
201
|
|
|
@@ -230,7 +230,7 @@ pnpm --dir packages/dsh-maestro-core test
|
|
|
230
230
|
pnpm --dir packages/dsh-maestro-core build
|
|
231
231
|
```
|
|
232
232
|
|
|
233
|
-
For daemon changes: `DSH_HOME=$(mktemp -d)
|
|
233
|
+
For daemon changes: `DSH_HOME=$(mktemp -d) "$DSH_BIN" web --port 0` + corrupt `settings.json` → assert `report` + `rollback`.
|
|
234
234
|
|
|
235
235
|
## Telegram
|
|
236
236
|
|
package/lib/debug-agent.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type DshLaunchDeps } from './dsh-launch.js';
|
|
1
2
|
export interface DebugAgentOpts {
|
|
2
3
|
reportPath: string;
|
|
3
4
|
health: {
|
|
@@ -14,4 +15,10 @@ export declare function runDebugAgent(opts: DebugAgentOpts): Promise<{
|
|
|
14
15
|
fixed: boolean;
|
|
15
16
|
reason: string;
|
|
16
17
|
}>;
|
|
18
|
+
/**
|
|
19
|
+
* The bounded dry-boot probe: boot `dsh web` on `tmp` as DSH_HOME and an
|
|
20
|
+
* ephemeral port, poll for 200, print ok/fail. The boot follows
|
|
21
|
+
* `resolveDshLaunch`, so a packaged host verifies the runtime it runs.
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildDryBootCommand(tmp: string, port: number, deps?: DshLaunchDeps): string;
|
|
17
24
|
export declare function _resetDebugAgentForTest(): void;
|
package/lib/debug-agent.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { resolveDshLaunch } from './dsh-launch.js';
|
|
1
2
|
let lastRun = 0;
|
|
2
3
|
let attempts = 0;
|
|
3
4
|
const MAX_ATTEMPTS = 3;
|
|
@@ -102,17 +103,25 @@ function defaultWriteFile(p, c) {
|
|
|
102
103
|
mkdirSync(dirname(p), { recursive: true });
|
|
103
104
|
writeFileSync(p, c, 'utf-8');
|
|
104
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* The bounded dry-boot probe: boot `dsh web` on `tmp` as DSH_HOME and an
|
|
108
|
+
* ephemeral port, poll for 200, print ok/fail. The boot follows
|
|
109
|
+
* `resolveDshLaunch`, so a packaged host verifies the runtime it runs.
|
|
110
|
+
*/
|
|
111
|
+
export function buildDryBootCommand(tmp, port, deps = {}) {
|
|
112
|
+
const launch = resolveDshLaunch(['web', '--port', String(port), '--no-open'], deps);
|
|
113
|
+
const argv = [launch.command, ...launch.args].map(a => JSON.stringify(a)).join(' ');
|
|
114
|
+
const boot = `${launch.cwd ? `cd ${JSON.stringify(launch.cwd)} && ` : ''}DSH_HOME=${tmp} ${argv} >${tmp}/dsh.log 2>&1 &`;
|
|
115
|
+
return `timeout 8 bash -c '${boot} pid=$!; for i in $(seq 1 5); do sleep 1; if curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${port}/ 2>&1 | grep -q 200; then echo ok; exit 0; fi; done; echo fail; exit 1'`;
|
|
116
|
+
}
|
|
105
117
|
async function defaultDryBoot() {
|
|
106
118
|
const { execSync } = await import('node:child_process');
|
|
107
|
-
const { resolveDeepseekHarnessDir } = await import('./paths.js');
|
|
108
119
|
const { buildKillStalePortsCommand } = await import('./restart-guards.js');
|
|
109
120
|
const port = Math.floor(19000 + Math.random() * 1000);
|
|
110
121
|
let tmp = '';
|
|
111
122
|
try {
|
|
112
|
-
const deepseekDir = resolveDeepseekHarnessDir();
|
|
113
123
|
tmp = execSync('mktemp -d', { encoding: 'utf-8' }).trim();
|
|
114
|
-
|
|
115
|
-
const out = execSync(`timeout 8 bash -c 'DSH_HOME=${tmp} pnpm --dir ${deepseekDir} dsh web --port ${port} --no-open >${tmp}/dsh.log 2>&1 & pid=$!; for i in $(seq 1 5); do sleep 1; if curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${port}/ 2>&1 | grep -q 200; then echo ok; exit 0; fi; done; echo fail; exit 1'`, { encoding: 'utf-8', timeout: 12000 });
|
|
124
|
+
const out = execSync(buildDryBootCommand(tmp, port), { encoding: 'utf-8', timeout: 12000 });
|
|
116
125
|
return out.includes('ok');
|
|
117
126
|
}
|
|
118
127
|
catch {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How to start the `dsh` CLI on this host. `DSH_BIN` (the installed package's
|
|
3
|
+
* bin, e.g. `~/.local/share/dsh/current/bin/dsh`) is the single switch: set,
|
|
4
|
+
* the packaged runtime runs through the current node; unset, the source
|
|
5
|
+
* checkout runs through tsx, which is how hosts without a packaged install
|
|
6
|
+
* have always booted.
|
|
7
|
+
*/
|
|
8
|
+
export interface DshLaunch {
|
|
9
|
+
command: string;
|
|
10
|
+
args: string[];
|
|
11
|
+
cwd?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface DshLaunchDeps {
|
|
14
|
+
env?: Record<string, string | undefined>;
|
|
15
|
+
execPath?: string;
|
|
16
|
+
sourceDir?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function resolveDshLaunch(args: string[], deps?: DshLaunchDeps): DshLaunch;
|
|
19
|
+
/** The same launch as a shell fragment (`[cd <dir> && ]exec <argv>`), every word quoted. */
|
|
20
|
+
export declare function dshLaunchShell(args: string[], deps?: DshLaunchDeps): string;
|
|
21
|
+
/**
|
|
22
|
+
* Whether a process command line is a `dsh web` boot in either form: the
|
|
23
|
+
* source entry (`apps/cli/src/bin.ts web`), the package entry (`lib/bin.js
|
|
24
|
+
* web`) or the installed bin (`.../dsh web`).
|
|
25
|
+
*/
|
|
26
|
+
export declare function isDshWebCommand(cmd: string): boolean;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { resolveDeepseekHarnessDir } from './paths.js';
|
|
2
|
+
export function resolveDshLaunch(args, deps = {}) {
|
|
3
|
+
const env = deps.env ?? process.env;
|
|
4
|
+
const command = deps.execPath ?? process.execPath;
|
|
5
|
+
const bin = env.DSH_BIN?.trim();
|
|
6
|
+
if (bin)
|
|
7
|
+
return { command, args: [bin, ...args] };
|
|
8
|
+
return {
|
|
9
|
+
command,
|
|
10
|
+
args: ['--import', 'tsx/esm', 'apps/cli/src/bin.ts', ...args],
|
|
11
|
+
cwd: deps.sourceDir ?? resolveDeepseekHarnessDir(),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** The same launch as a shell fragment (`[cd <dir> && ]exec <argv>`), every word quoted. */
|
|
15
|
+
export function dshLaunchShell(args, deps = {}) {
|
|
16
|
+
const launch = resolveDshLaunch(args, deps);
|
|
17
|
+
const argv = [launch.command, ...launch.args].map(a => JSON.stringify(a)).join(' ');
|
|
18
|
+
return launch.cwd ? `cd ${JSON.stringify(launch.cwd)} && exec ${argv}` : `exec ${argv}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Whether a process command line is a `dsh web` boot in either form: the
|
|
22
|
+
* source entry (`apps/cli/src/bin.ts web`), the package entry (`lib/bin.js
|
|
23
|
+
* web`) or the installed bin (`.../dsh web`).
|
|
24
|
+
*/
|
|
25
|
+
export function isDshWebCommand(cmd) {
|
|
26
|
+
return /(?:\bbin\.[jt]s|\/dsh)\s+web(?:\s|$)/.test(cmd);
|
|
27
|
+
}
|
package/lib/restart-tool.d.ts
CHANGED
|
@@ -53,7 +53,9 @@ export declare function listDryBootCandidates(readers?: GcReaders): DryBootCandi
|
|
|
53
53
|
* Boot a copy of the live web profile on an isolated DSH_HOME and verify the
|
|
54
54
|
* plugin tree loads and serves. Returns ok + a one-line detail for the tool
|
|
55
55
|
* message. The spawned tree is killed (best-effort) and the temp home removed.
|
|
56
|
-
* Unit tests mock this (never spawn a real node boot in tests).
|
|
56
|
+
* Unit tests mock this (never spawn a real node boot in tests). The boot uses
|
|
57
|
+
* `resolveDshLaunch`, so `harnessRoot` (the source checkout) only matters
|
|
58
|
+
* when `DSH_BIN` is unset.
|
|
57
59
|
*
|
|
58
60
|
* NOTE (bin.ts finding): the `web` alias already implies `--profile web`, and
|
|
59
61
|
* the web app's own commander program (no allowUnknownOption) rejects a stray
|
package/lib/restart-tool.js
CHANGED
|
@@ -19,6 +19,7 @@ import { createRequire } from 'node:module';
|
|
|
19
19
|
import { writeRestartRequest, readRestartRequest } from './restart-guards.js';
|
|
20
20
|
import { intentPath, readIntent, readRestartOutcome } from './intents.js';
|
|
21
21
|
import { supervisorDaemonState, describeDaemonState } from './daemon-freshness.js';
|
|
22
|
+
import { resolveDshLaunch, isDshWebCommand } from './dsh-launch.js';
|
|
22
23
|
/**
|
|
23
24
|
* Copy a live profile tree for an isolated dry-boot. A naive recursive copy
|
|
24
25
|
* breaks `link:` installs: their node_modules entries are relative symlinks
|
|
@@ -104,7 +105,7 @@ export function listDryBootCandidates(readers = {}) {
|
|
|
104
105
|
for (const p of readProc()) {
|
|
105
106
|
if (p.pid === selfPid)
|
|
106
107
|
continue;
|
|
107
|
-
if (
|
|
108
|
+
if (!isDshWebCommand(p.cmd))
|
|
108
109
|
continue;
|
|
109
110
|
const home = /^DSH_HOME=([^\0]*)/m.exec(p.env)?.[1] ?? '';
|
|
110
111
|
if (!home.startsWith(join(tmpdir(), 'dsh-dryboot-')))
|
|
@@ -123,7 +124,9 @@ export function listDryBootCandidates(readers = {}) {
|
|
|
123
124
|
* Boot a copy of the live web profile on an isolated DSH_HOME and verify the
|
|
124
125
|
* plugin tree loads and serves. Returns ok + a one-line detail for the tool
|
|
125
126
|
* message. The spawned tree is killed (best-effort) and the temp home removed.
|
|
126
|
-
* 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.
|
|
127
130
|
*
|
|
128
131
|
* NOTE (bin.ts finding): the `web` alias already implies `--profile web`, and
|
|
129
132
|
* the web app's own commander program (no allowUnknownOption) rejects a stray
|
|
@@ -143,8 +146,9 @@ export async function dryBootVerify(harnessRoot, opts = {}) {
|
|
|
143
146
|
copyProfileForDryBoot(liveProfile, join(tmpHome, 'profiles', 'web'));
|
|
144
147
|
const port = String(9000 + Math.floor(Math.random() * 1000));
|
|
145
148
|
const url = `http://127.0.0.1:${port}/`;
|
|
146
|
-
|
|
147
|
-
|
|
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'],
|
|
148
152
|
});
|
|
149
153
|
child.stdout?.on('data', (d) => logs.push(d.toString()));
|
|
150
154
|
child.stderr?.on('data', (d) => logs.push(d.toString()));
|
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 {
|
|
@@ -24,3 +25,5 @@ export interface RestartWebResult {
|
|
|
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,7 +3,7 @@ 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
|
/**
|
|
@@ -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/package.json
CHANGED
|
@@ -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
|
|
@@ -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
|
package/src/host/debug-agent.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { resolveDshLaunch, type DshLaunchDeps } from './dsh-launch.js'
|
|
2
|
+
|
|
1
3
|
export interface DebugAgentOpts {
|
|
2
4
|
reportPath: string
|
|
3
5
|
health: { error?: string; httpCode?: number }
|
|
@@ -109,20 +111,26 @@ function defaultWriteFile(p: string, c: string): void {
|
|
|
109
111
|
writeFileSync(p, c, 'utf-8')
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/**
|
|
115
|
+
* The bounded dry-boot probe: boot `dsh web` on `tmp` as DSH_HOME and an
|
|
116
|
+
* ephemeral port, poll for 200, print ok/fail. The boot follows
|
|
117
|
+
* `resolveDshLaunch`, so a packaged host verifies the runtime it runs.
|
|
118
|
+
*/
|
|
119
|
+
export function buildDryBootCommand(tmp: string, port: number, deps: DshLaunchDeps = {}): string {
|
|
120
|
+
const launch = resolveDshLaunch(['web', '--port', String(port), '--no-open'], deps)
|
|
121
|
+
const argv = [launch.command, ...launch.args].map(a => JSON.stringify(a)).join(' ')
|
|
122
|
+
const boot = `${launch.cwd ? `cd ${JSON.stringify(launch.cwd)} && ` : ''}DSH_HOME=${tmp} ${argv} >${tmp}/dsh.log 2>&1 &`
|
|
123
|
+
return `timeout 8 bash -c '${boot} pid=$!; for i in $(seq 1 5); do sleep 1; if curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${port}/ 2>&1 | grep -q 200; then echo ok; exit 0; fi; done; echo fail; exit 1'`
|
|
124
|
+
}
|
|
125
|
+
|
|
112
126
|
async function defaultDryBoot(): Promise<boolean> {
|
|
113
127
|
const { execSync } = await import('node:child_process')
|
|
114
|
-
const { resolveDeepseekHarnessDir } = await import('./paths.js')
|
|
115
128
|
const { buildKillStalePortsCommand } = await import('./restart-guards.js')
|
|
116
129
|
const port = Math.floor(19000 + Math.random() * 1000)
|
|
117
130
|
let tmp = ''
|
|
118
131
|
try {
|
|
119
|
-
const deepseekDir = resolveDeepseekHarnessDir()
|
|
120
132
|
tmp = execSync('mktemp -d', { encoding: 'utf-8' }).trim()
|
|
121
|
-
|
|
122
|
-
const out = execSync(
|
|
123
|
-
`timeout 8 bash -c 'DSH_HOME=${tmp} pnpm --dir ${deepseekDir} dsh web --port ${port} --no-open >${tmp}/dsh.log 2>&1 & pid=$!; for i in $(seq 1 5); do sleep 1; if curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${port}/ 2>&1 | grep -q 200; then echo ok; exit 0; fi; done; echo fail; exit 1'`,
|
|
124
|
-
{ encoding: 'utf-8', timeout: 12000 },
|
|
125
|
-
)
|
|
133
|
+
const out = execSync(buildDryBootCommand(tmp, port), { encoding: 'utf-8', timeout: 12000 })
|
|
126
134
|
return out.includes('ok')
|
|
127
135
|
} catch {
|
|
128
136
|
return false
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { resolveDeepseekHarnessDir } from './paths.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How to start the `dsh` CLI on this host. `DSH_BIN` (the installed package's
|
|
5
|
+
* bin, e.g. `~/.local/share/dsh/current/bin/dsh`) is the single switch: set,
|
|
6
|
+
* the packaged runtime runs through the current node; unset, the source
|
|
7
|
+
* checkout runs through tsx, which is how hosts without a packaged install
|
|
8
|
+
* have always booted.
|
|
9
|
+
*/
|
|
10
|
+
export interface DshLaunch {
|
|
11
|
+
command: string
|
|
12
|
+
args: string[]
|
|
13
|
+
cwd?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface DshLaunchDeps {
|
|
17
|
+
env?: Record<string, string | undefined>
|
|
18
|
+
execPath?: string
|
|
19
|
+
sourceDir?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function resolveDshLaunch(args: string[], deps: DshLaunchDeps = {}): DshLaunch {
|
|
23
|
+
const env = deps.env ?? process.env
|
|
24
|
+
const command = deps.execPath ?? process.execPath
|
|
25
|
+
const bin = env.DSH_BIN?.trim()
|
|
26
|
+
if (bin) return { command, args: [bin, ...args] }
|
|
27
|
+
return {
|
|
28
|
+
command,
|
|
29
|
+
args: ['--import', 'tsx/esm', 'apps/cli/src/bin.ts', ...args],
|
|
30
|
+
cwd: deps.sourceDir ?? resolveDeepseekHarnessDir(),
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The same launch as a shell fragment (`[cd <dir> && ]exec <argv>`), every word quoted. */
|
|
35
|
+
export function dshLaunchShell(args: string[], deps: DshLaunchDeps = {}): string {
|
|
36
|
+
const launch = resolveDshLaunch(args, deps)
|
|
37
|
+
const argv = [launch.command, ...launch.args].map(a => JSON.stringify(a)).join(' ')
|
|
38
|
+
return launch.cwd ? `cd ${JSON.stringify(launch.cwd)} && exec ${argv}` : `exec ${argv}`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether a process command line is a `dsh web` boot in either form: the
|
|
43
|
+
* source entry (`apps/cli/src/bin.ts web`), the package entry (`lib/bin.js
|
|
44
|
+
* web`) or the installed bin (`.../dsh web`).
|
|
45
|
+
*/
|
|
46
|
+
export function isDshWebCommand(cmd: string): boolean {
|
|
47
|
+
return /(?:\bbin\.[jt]s|\/dsh)\s+web(?:\s|$)/.test(cmd)
|
|
48
|
+
}
|
package/src/host/restart-tool.ts
CHANGED
|
@@ -20,6 +20,7 @@ import { createRequire } from 'node:module'
|
|
|
20
20
|
import { writeRestartRequest, readRestartRequest } from './restart-guards.js'
|
|
21
21
|
import { intentPath, readIntent, readRestartOutcome } from './intents.js'
|
|
22
22
|
import { supervisorDaemonState, describeDaemonState, type DaemonState } from './daemon-freshness.js'
|
|
23
|
+
import { resolveDshLaunch, isDshWebCommand } from './dsh-launch.js'
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Copy a live profile tree for an isolated dry-boot. A naive recursive copy
|
|
@@ -109,7 +110,7 @@ export function listDryBootCandidates(readers: GcReaders = {}): DryBootCandidate
|
|
|
109
110
|
const out: DryBootCandidate[] = []
|
|
110
111
|
for (const p of readProc()) {
|
|
111
112
|
if (p.pid === selfPid) continue
|
|
112
|
-
if (
|
|
113
|
+
if (!isDshWebCommand(p.cmd)) continue
|
|
113
114
|
const home = /^DSH_HOME=([^\0]*)/m.exec(p.env)?.[1] ?? ''
|
|
114
115
|
if (!home.startsWith(join(tmpdir(), 'dsh-dryboot-'))) continue
|
|
115
116
|
const ports = ssPortsOf(p.pid)
|
|
@@ -125,7 +126,9 @@ export function listDryBootCandidates(readers: GcReaders = {}): DryBootCandidate
|
|
|
125
126
|
* Boot a copy of the live web profile on an isolated DSH_HOME and verify the
|
|
126
127
|
* plugin tree loads and serves. Returns ok + a one-line detail for the tool
|
|
127
128
|
* message. The spawned tree is killed (best-effort) and the temp home removed.
|
|
128
|
-
* Unit tests mock this (never spawn a real node boot in tests).
|
|
129
|
+
* Unit tests mock this (never spawn a real node boot in tests). The boot uses
|
|
130
|
+
* `resolveDshLaunch`, so `harnessRoot` (the source checkout) only matters
|
|
131
|
+
* when `DSH_BIN` is unset.
|
|
129
132
|
*
|
|
130
133
|
* NOTE (bin.ts finding): the `web` alias already implies `--profile web`, and
|
|
131
134
|
* the web app's own commander program (no allowUnknownOption) rejects a stray
|
|
@@ -144,8 +147,9 @@ export async function dryBootVerify(harnessRoot: string, opts: { timeoutMs?: num
|
|
|
144
147
|
copyProfileForDryBoot(liveProfile, join(tmpHome, 'profiles', 'web'))
|
|
145
148
|
const port = String(9000 + Math.floor(Math.random() * 1000))
|
|
146
149
|
const url = `http://127.0.0.1:${port}/`
|
|
147
|
-
|
|
148
|
-
|
|
150
|
+
const launch = resolveDshLaunch(['web', '--no-open', '--port', port], { sourceDir: harnessRoot })
|
|
151
|
+
child = spawn(launch.command, launch.args, {
|
|
152
|
+
cwd: launch.cwd, env: { ...process.env, DSH_HOME: tmpHome }, stdio: ['ignore', 'pipe', 'pipe'],
|
|
149
153
|
})
|
|
150
154
|
child.stdout?.on('data', (d: Buffer) => logs.push(d.toString()))
|
|
151
155
|
child.stderr?.on('data', (d: Buffer) => logs.push(d.toString()))
|
package/src/host/restart-web.ts
CHANGED
|
@@ -3,7 +3,7 @@ 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, type BootLockDeps } from './boot-lock.js'
|
|
6
|
-
import {
|
|
6
|
+
import { dshLaunchShell, type DshLaunchDeps } from './dsh-launch.js'
|
|
7
7
|
|
|
8
8
|
/** Boot budget: how long a boot may take before its failures are judged. */
|
|
9
9
|
export const DEFAULT_BOOT_GRACE_MS = 180_000
|
|
@@ -61,12 +61,14 @@ export async function performSingleBootRestart(deps: RestartWebDeps = {}): Promi
|
|
|
61
61
|
return { restarted: true }
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
/** The detached last-resort start, appending to the web log. Exported for tests. */
|
|
65
|
+
export function buildNohupCommand(logPath: string, deps: DshLaunchDeps = {}): string {
|
|
66
|
+
const launch = dshLaunchShell(['web', '--no-open'], deps)
|
|
67
|
+
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' &`
|
|
68
|
+
}
|
|
69
|
+
|
|
64
70
|
async function defaultNohup(): Promise<void> {
|
|
65
71
|
const { execSync } = await import('node:child_process')
|
|
66
|
-
const harnessRoot = resolveDeepseekHarnessDir()
|
|
67
72
|
const logPath = path.join(os.homedir(), '.dsh/dsh-web.log')
|
|
68
|
-
execSync(
|
|
69
|
-
`setsid nohup bash -c 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; cd ${JSON.stringify(harnessRoot)} && exec node --import tsx/esm apps/cli/src/bin.ts web --no-open >> ${JSON.stringify(logPath)} 2>&1' &`,
|
|
70
|
-
{ timeout: 5000 },
|
|
71
|
-
)
|
|
73
|
+
execSync(buildNohupCommand(logPath), { timeout: 5000 })
|
|
72
74
|
}
|
|
@@ -11,6 +11,8 @@ ExecStart=/bin/bash -c 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] &&
|
|
|
11
11
|
Restart=always
|
|
12
12
|
RestartSec=2
|
|
13
13
|
Environment=DSH_HOME=%h/.dsh
|
|
14
|
+
# The daemon's nohup fallback and debug-agent dry-boot launch dsh through DSH_BIN.
|
|
15
|
+
Environment=DSH_BIN=%h/.local/share/dsh/current/bin/dsh
|
|
14
16
|
# Optional Telegram — loose mode: set these if you want Telegram alerts, otherwise daemon logs only
|
|
15
17
|
# Environment=TELEGRAM_BOT_TOKEN=your_bot_token
|
|
16
18
|
# Environment=TELEGRAM_CHAT_ID=your_chat_id
|
|
@@ -9,11 +9,17 @@ StartLimitBurst=3
|
|
|
9
9
|
|
|
10
10
|
[Service]
|
|
11
11
|
Type=simple
|
|
12
|
-
|
|
12
|
+
# Runs the published @deepseek-ai/dsh package, installed as one npm prefix per
|
|
13
|
+
# version under ~/.local/share/dsh/<version> with `current` linking the active
|
|
14
|
+
# one. DSH_BIN also steers this package's restart gate and dry-boots.
|
|
15
|
+
# The working directory is not neutral: dsh loads <cwd>/.env as the project env
|
|
16
|
+
# layer, so it stays on the install root, which holds no .env.
|
|
17
|
+
WorkingDirectory=%h/.local/share/dsh
|
|
18
|
+
Environment=DSH_BIN=%h/.local/share/dsh/current/bin/dsh
|
|
13
19
|
# Portable: sources nvm directly (bash -lc alone fails — ~/.bashrc exits early for
|
|
14
20
|
# non-interactive shells via `case $- in *i*) ;; *) return;; esac`, so nvm never loads).
|
|
15
21
|
# Falls back to system node for machines without nvm — no hardcoded /v24.19.0 version.
|
|
16
|
-
ExecStart=/bin/bash -c 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; exec node
|
|
22
|
+
ExecStart=/bin/bash -c 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; exec node "$DSH_BIN" web --no-open'
|
|
17
23
|
Restart=always
|
|
18
24
|
RestartSec=5
|
|
19
25
|
# Wait (up to 30 x 5s) for the previous instance to release the raw webserver
|