@dsh-plugin/dsh-pack-plugin 0.0.0-stage → 0.0.1
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 +178 -3
- package/cordis.patch.yml +9 -0
- package/icons/folder-zip-line.svg +1 -0
- package/lib/ball.js +597 -0
- package/lib/client.js +2934 -0
- package/lib/host.js +51745 -0
- package/lib/migrate-helper.js +35346 -0
- package/lib/progress-main.cjs +79 -0
- package/lib/progress-window.ps1 +114 -0
- package/lib/progress.html +126 -0
- package/lib/publish-to-github/SKILL.md +132 -0
- package/package.json +77 -4
- package/src/ball-entry.js +5 -0
- package/src/ball-host.js +99 -0
- package/src/ball-settings.js +352 -0
- package/src/ball.js +418 -0
- package/src/channel.js +12 -0
- package/src/client-plugin.js +58 -0
- package/src/client-rpc.js +14 -0
- package/src/client.js +5 -0
- package/src/core/discovery.js +192 -0
- package/src/core/dspack.js +93 -0
- package/src/core/extension-files.js +102 -0
- package/src/core/extension-install.js +295 -0
- package/src/core/extension-patch.js +369 -0
- package/src/core/extension-skills.js +164 -0
- package/src/core/extension-validate.js +127 -0
- package/src/core/extensions.js +207 -0
- package/src/core/home-store.js +32 -0
- package/src/core/host.js +149 -0
- package/src/core/index.js +59 -0
- package/src/core/inspect.js +162 -0
- package/src/core/install.js +775 -0
- package/src/core/managed-install.js +84 -0
- package/src/core/manifest.js +717 -0
- package/src/core/market.js +268 -0
- package/src/core/mcp-composition.js +189 -0
- package/src/core/mcp-install.js +112 -0
- package/src/core/mcp-transport.js +116 -0
- package/src/core/pack.js +371 -0
- package/src/core/plugin-metadata.js +355 -0
- package/src/core/registry.js +316 -0
- package/src/core/repo.js +339 -0
- package/src/core/scan.js +78 -0
- package/src/core/security.js +101 -0
- package/src/core/special.js +50 -0
- package/src/core/tar.js +103 -0
- package/src/core/update-config.js +234 -0
- package/src/core/update.js +792 -0
- package/src/core/vendored.js +715 -0
- package/src/core/workspace.js +95 -0
- package/src/endpoints.js +455 -0
- package/src/ensure-manager.js +186 -0
- package/src/host-node.js +625 -0
- package/src/host.js +17 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +39 -0
- package/src/junction.js +142 -0
- package/src/migrate-helper.js +225 -0
- package/src/migrate.js +56 -0
- package/src/packaged.js +9 -0
- package/src/profiles.js +106 -0
- package/src/progress-main.cjs +79 -0
- package/src/progress-window.ps1 +114 -0
- package/src/progress.html +126 -0
- package/src/progress.js +132 -0
- package/src/rpc.js +157 -0
- package/src/runtime.js +43 -0
- package/src/section-gate.js +50 -0
- package/src/settings-nav-icon.js +188 -0
- package/src/settings.js +1611 -0
- package/src/skills/index.js +90 -0
- package/src/skills/publish-to-github/SKILL.md +132 -0
- package/src/tasks.js +145 -0
- package/src/v6-dialogs.js +312 -0
package/src/junction.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// 机制 A(推荐):junction 换指 —— 切换 profile 只改 desktop 这个指针,无 EBUSY、无需退出。
|
|
2
|
+
// 机制 B(兜底):junction 不可用(非 NTFS 等)时,脱管切换进程改名 + 重启(后续接线)。
|
|
3
|
+
//
|
|
4
|
+
// 安全红线:删除 junction 必须用 unlink(lstat 语义),绝不能用 rm -r ——
|
|
5
|
+
// 后者在 Windows 上会递归删掉 junction **目标**目录的内容。
|
|
6
|
+
import fsp from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { ACTIVE_NAME } from './runtime.js';
|
|
9
|
+
import { readState, writeState, resolveActiveName } from './profiles.js';
|
|
10
|
+
import { spawnMigrationHelper } from './migrate.js';
|
|
11
|
+
import { ensureManagerInProfile } from './ensure-manager.js';
|
|
12
|
+
import { spawnProgressWindow, writeProgress, buildSteps, setStep, setPhase, HINT } from './progress.js';
|
|
13
|
+
import { HOME_ARTIFACT_STORE } from './core/home-store.js';
|
|
14
|
+
|
|
15
|
+
const JUNCTION_KIND = process.platform === 'win32' ? 'junction' : 'dir';
|
|
16
|
+
const STASH_DIR = '.dsh-pack';
|
|
17
|
+
|
|
18
|
+
async function lstat(p) {
|
|
19
|
+
try {
|
|
20
|
+
return await fsp.lstat(p);
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function isDir(p) {
|
|
27
|
+
const s = await lstat(p);
|
|
28
|
+
return !!s && (s.isDirectory() || s.isSymbolicLink());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function exists(p) {
|
|
32
|
+
return (await lstat(p)) !== null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 把激活指针 desktop 换到目标 profile。
|
|
37
|
+
* @returns {Promise<{active:string, method:'junction', requiresRestart:true, previous:string}>}
|
|
38
|
+
*/
|
|
39
|
+
export async function switchProfile(runtime, target, { swapSkills = true, allowDelegate = true, onProgress, managerSource = 'npm' } = {}) {
|
|
40
|
+
const { profilesDir, home } = runtime;
|
|
41
|
+
const desktop = path.join(profilesDir, ACTIVE_NAME);
|
|
42
|
+
const targetDir = path.join(profilesDir, target);
|
|
43
|
+
|
|
44
|
+
if (target === ACTIVE_NAME) {
|
|
45
|
+
throw new Error(`「${ACTIVE_NAME}」是官方硬编码的激活指针名,不能作为 profile 名`);
|
|
46
|
+
}
|
|
47
|
+
if (!(await isDir(targetDir))) {
|
|
48
|
+
throw new Error(`profile「${target}」不存在(${targetDir})`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const previous = (await resolveActiveName(runtime)) ?? 'default';
|
|
52
|
+
|
|
53
|
+
// 1) 每次切换都走脱管 helper:杀桌面 → 换指 → 自动重启(体验一致,不只首次迁移)。
|
|
54
|
+
// 先派进度窗口 + 落初始进度,再迁装管理器(带进度),最后派生 helper。
|
|
55
|
+
// helper 自身(allowDelegate=false)或非 win32 走下面的原地换指。
|
|
56
|
+
if (allowDelegate && process.platform === 'win32') {
|
|
57
|
+
const st = await lstat(desktop);
|
|
58
|
+
const firstTime = !(st && st.isSymbolicLink()); // 还是真实目录 → 首次切换
|
|
59
|
+
const progressPid = spawnProgressWindow(home);
|
|
60
|
+
await writeProgress(home, { from: previous, to: target, firstTime, phase: 'running', steps: buildSteps(firstTime), hint: HINT });
|
|
61
|
+
try {
|
|
62
|
+
await setStep(home, 'install', 'running');
|
|
63
|
+
await ensureManagerInProfile(runtime, target, { source: managerSource });
|
|
64
|
+
await setStep(home, 'install', 'done');
|
|
65
|
+
} catch (err) {
|
|
66
|
+
await setPhase(home, 'failed', err?.message ?? String(err));
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
spawnMigrationHelper(runtime, target, { progressPid });
|
|
70
|
+
return { active: target, method: 'junction', requiresRestart: true, previous, restarting: true };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 2) 原地换指(非 win32 / helper 自身):先迁装管理器,首次把 desktop 存档为 default,再建指针。
|
|
74
|
+
// helper 已在杀桌面后调用,rename 即便 EBUSY 也会被其轮询重试(LOCK_CODES 见 migrate-helper.js)。
|
|
75
|
+
await ensureManagerInProfile(runtime, target, { source: managerSource });
|
|
76
|
+
let st = await lstat(desktop);
|
|
77
|
+
if (st && !st.isSymbolicLink()) {
|
|
78
|
+
const defaultDir = path.join(profilesDir, 'default');
|
|
79
|
+
if (await exists(defaultDir)) {
|
|
80
|
+
throw new Error('profiles/default 已存在,无法把 desktop 存档为 default');
|
|
81
|
+
}
|
|
82
|
+
onProgress?.('move', 'running');
|
|
83
|
+
await fsp.rename(desktop, defaultDir);
|
|
84
|
+
onProgress?.('move', 'done');
|
|
85
|
+
st = null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 3) 删旧指针 → 建新指针(junction 用绝对目标)。
|
|
89
|
+
onProgress?.('link', 'running');
|
|
90
|
+
if (st && st.isSymbolicLink()) {
|
|
91
|
+
await fsp.unlink(desktop); // 红线:unlink,不是 rm -r
|
|
92
|
+
}
|
|
93
|
+
await fsp.symlink(targetDir, desktop, JUNCTION_KIND);
|
|
94
|
+
onProgress?.('link', 'done');
|
|
95
|
+
|
|
96
|
+
// 3) home 级 skills / .agent-presets 换指(各 artifact 独立判断首次迁移,与 desktop 解耦)。
|
|
97
|
+
if (swapSkills) {
|
|
98
|
+
for (const artifact of Object.keys(HOME_ARTIFACT_STORE)) {
|
|
99
|
+
await repointHomeArtifact(home, artifact, previous, target);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 4) 落 state。
|
|
104
|
+
const state = await readState(home);
|
|
105
|
+
state.activeProfile = target;
|
|
106
|
+
state.desktopIsJunction = true;
|
|
107
|
+
await writeState(home, state);
|
|
108
|
+
|
|
109
|
+
return { active: target, method: 'junction', requiresRestart: true, previous };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// skills / .agent-presets 是 home 级($DSH_HOME/<artifact>),不在 profile 目录里(官方源码确证)。
|
|
113
|
+
// 切换 = 把 <artifact> 这个 junction 指到 .dsh-pack/<store>/<target>;首次切换时若它还是真实目录,
|
|
114
|
+
// 先存档到 .dsh-pack/<store>/<previous>(活目录优先:rm -r 掉旧 stash/同名 slot 再 rename)。
|
|
115
|
+
// 每个 artifact 各自 lstat 判断,与 desktop 的 firstTime 解耦——desktop 已迁、skills 未迁也能各迁各的。
|
|
116
|
+
async function repointHomeArtifact(home, artifact, previousName, targetName) {
|
|
117
|
+
const store = HOME_ARTIFACT_STORE[artifact];
|
|
118
|
+
const link = path.join(home, artifact);
|
|
119
|
+
const storeDir = path.join(home, STASH_DIR, store);
|
|
120
|
+
const target = path.join(storeDir, targetName);
|
|
121
|
+
|
|
122
|
+
const st = await lstat(link);
|
|
123
|
+
if (st && !st.isSymbolicLink()) {
|
|
124
|
+
// 首次迁移:真实目录存档到「上一个 profile」名下。
|
|
125
|
+
const prev = path.join(storeDir, previousName || 'default');
|
|
126
|
+
await fsp.mkdir(storeDir, { recursive: true });
|
|
127
|
+
await fsp.rm(prev, { recursive: true, force: true });
|
|
128
|
+
await fsp.rename(link, prev);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 换指:删旧指针(红线 unlink,不是 rm -r)→ 保证目标 slot 存在 → 建 junction(绝对目标)。
|
|
132
|
+
if (st && st.isSymbolicLink()) {
|
|
133
|
+
await fsp.unlink(link);
|
|
134
|
+
}
|
|
135
|
+
await fsp.mkdir(target, { recursive: true });
|
|
136
|
+
await fsp.symlink(target, link, JUNCTION_KIND);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 机制 B(兜底):junction 不可用时的脱管切换器。先走机制 A,此路径后续接线。 */
|
|
140
|
+
export async function switchProfileViaHelper(_runtime, _target) {
|
|
141
|
+
throw new Error('机制 B(脱管切换器)尚未接线,请先用 junction(机制 A)');
|
|
142
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// 脱管迁移进程(机制 B 执行体)。
|
|
2
|
+
//
|
|
3
|
+
// 由 migrate.js 用 `ELECTRON_RUN_AS_NODE=1 process.execPath 本文件` 派生,与宿主进程树脱钩。
|
|
4
|
+
// 流程:留出响应 flush 宽限 → 杀 electron 主进程树 → 轮询 rename(desktop→default) 直到锁释放 →
|
|
5
|
+
// switchProfile 完成建 junction + 落 state → 重启桌面 → 退出。
|
|
6
|
+
import fsp from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import http from 'node:http';
|
|
9
|
+
import { spawn, execFile } from 'node:child_process';
|
|
10
|
+
import { switchProfile } from './junction.js';
|
|
11
|
+
import { setStep, setPhase } from './progress.js';
|
|
12
|
+
|
|
13
|
+
// —— 参数解析(--key=value,值由 spawn 原样传入,无 shell 拆词)——
|
|
14
|
+
const argv = {};
|
|
15
|
+
for (const a of process.argv.slice(2)) {
|
|
16
|
+
const m = /^--([^=]+)=(.*)$/.exec(a);
|
|
17
|
+
if (m) argv[m[1]] = m[2];
|
|
18
|
+
}
|
|
19
|
+
const electronPid = Number(argv['electron-pid']) || 0;
|
|
20
|
+
const profilesDir = argv['profiles-dir'];
|
|
21
|
+
const home = argv['home'];
|
|
22
|
+
const target = argv['target'];
|
|
23
|
+
const relaunchCmd = argv['relaunch-cmd'] ?? '';
|
|
24
|
+
const relaunchCwd = argv['relaunch-cwd'] ?? '';
|
|
25
|
+
const progressPid = Number(argv['progress-pid']) || 0;
|
|
26
|
+
|
|
27
|
+
const LOG = path.join(home, '.dsh-pack', 'migrate.log');
|
|
28
|
+
async function log(msg) {
|
|
29
|
+
try {
|
|
30
|
+
await fsp.mkdir(path.dirname(LOG), { recursive: true });
|
|
31
|
+
await fsp.appendFile(LOG, `[${new Date().toISOString()}] ${msg}\n`);
|
|
32
|
+
} catch { /* 日志失败不影响迁移 */ }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
36
|
+
const LOCK_CODES = ['EBUSY', 'EPERM', 'EACCES'];
|
|
37
|
+
|
|
38
|
+
function run(cmd, args, opts = {}) {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
execFile(cmd, args, { windowsHide: true, ...opts }, (err, stdout, stderr) => {
|
|
41
|
+
resolve({ err, stdout: (stdout || '').trim(), stderr: (stderr || '').trim() });
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function captureCommandLine(pid) {
|
|
47
|
+
if (process.platform !== 'win32' || !pid) return null;
|
|
48
|
+
const script = `Get-CimInstance Win32_Process -Filter "ProcessId=${pid}" | Select-Object -ExpandProperty CommandLine`;
|
|
49
|
+
const r = await run('powershell', ['-NoProfile', '-Command', script]);
|
|
50
|
+
return r.err ? null : (r.stdout || null);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 杀整条桌面启动链。绝不能用 taskkill /T(会连 helper 自己一起杀);也不能只杀 electron
|
|
54
|
+
// 主进程后代——锁其实被启动器 tsx scripts/dev.ts(electron 主进程的父)握着,它要慢慢退出
|
|
55
|
+
// 才释放锁。正确做法:从主进程沿父链上溯到启动器链顶端(node/electron/cmd),杀整条链的树,
|
|
56
|
+
// 仍排除 helper 自身与运行本脚本的 powershell,并停在 bash(调用方的 shell)之前。
|
|
57
|
+
function killScript(mainPid, selfPid, progressPid) {
|
|
58
|
+
return `
|
|
59
|
+
$main = ${mainPid}
|
|
60
|
+
$self = ${selfPid}
|
|
61
|
+
$progress = ${progressPid}
|
|
62
|
+
$all = @(Get-CimInstance Win32_Process)
|
|
63
|
+
$parent = @{}; $name = @{}
|
|
64
|
+
foreach ($p in $all) { $parent[$p.ProcessId] = $p.ParentProcessId; $name[$p.ProcessId] = $p.Name }
|
|
65
|
+
$root = $main
|
|
66
|
+
$cur = $main
|
|
67
|
+
while ($true) {
|
|
68
|
+
$par = $parent[$cur]
|
|
69
|
+
if (-not $par) { break }
|
|
70
|
+
if ($name[$par] -notmatch '^(node|electron|cmd|conhost)') { break }
|
|
71
|
+
$root = $par
|
|
72
|
+
$cur = $par
|
|
73
|
+
}
|
|
74
|
+
# 进度窗口是 electron GUI,会派生子进程(renderer/GPU/utility);只排除它的主 pid 不够,
|
|
75
|
+
# 那些子进程会被当桌面后代一起杀掉、窗口随之消失。这里算出 $progress 整棵子树一并跳过。
|
|
76
|
+
$skip = New-Object 'System.Collections.Generic.HashSet[int]'
|
|
77
|
+
[void]$skip.Add($progress)
|
|
78
|
+
$pq = New-Object 'System.Collections.Generic.List[int]'
|
|
79
|
+
$pq.Add($progress)
|
|
80
|
+
while ($pq.Count -gt 0) {
|
|
81
|
+
$n = $pq[0]; $pq.RemoveAt(0)
|
|
82
|
+
foreach ($p in $all) {
|
|
83
|
+
if ($parent[$p.ProcessId] -eq $n -and -not $skip.Contains($p.ProcessId)) {
|
|
84
|
+
[void]$skip.Add($p.ProcessId); $pq.Add($p.ProcessId)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
$desc = New-Object 'System.Collections.Generic.List[int]'
|
|
89
|
+
$q = New-Object 'System.Collections.Generic.List[int]'
|
|
90
|
+
$q.Add($root)
|
|
91
|
+
while ($q.Count -gt 0) {
|
|
92
|
+
$n = $q[0]; $q.RemoveAt(0)
|
|
93
|
+
foreach ($p in $all) {
|
|
94
|
+
if ($parent[$p.ProcessId] -eq $n) { $desc.Add($p.ProcessId); $q.Add($p.ProcessId) }
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
foreach ($p in $desc) {
|
|
98
|
+
if ($p -eq $self -or $p -eq $PID -or $skip.Contains($p)) { continue }
|
|
99
|
+
Stop-Process -Id $p -Force -ErrorAction SilentlyContinue
|
|
100
|
+
}
|
|
101
|
+
Stop-Process -Id $root -Force -ErrorAction SilentlyContinue
|
|
102
|
+
`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function killElectronTree(mainPid) {
|
|
106
|
+
if (process.platform === 'win32' && mainPid) {
|
|
107
|
+
await run('powershell', ['-NoProfile', '-Command', killScript(mainPid, process.pid, progressPid)]);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (mainPid) { try { process.kill(mainPid, 'SIGTERM'); } catch { /* 已退出 */ } }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 轮询 switchProfile 直到锁释放(桌面被 kill 后 rename 才不再 EBUSY)。 */
|
|
114
|
+
async function waitForLockRelease() {
|
|
115
|
+
const runtime = { profilesDir, home };
|
|
116
|
+
const deadline = Date.now() + 60000;
|
|
117
|
+
let attempts = 0;
|
|
118
|
+
for (;;) {
|
|
119
|
+
try {
|
|
120
|
+
return await switchProfile(runtime, target, {
|
|
121
|
+
swapSkills: true,
|
|
122
|
+
allowDelegate: false,
|
|
123
|
+
onProgress: (id, status) => { void setStep(home, id, status); },
|
|
124
|
+
});
|
|
125
|
+
} catch (err) {
|
|
126
|
+
attempts++;
|
|
127
|
+
if (attempts === 1 || attempts % 10 === 0) {
|
|
128
|
+
await log(`retry #${attempts} code=${err?.code} msg=${err?.message}`);
|
|
129
|
+
}
|
|
130
|
+
if (!LOCK_CODES.includes(err?.code)) throw err;
|
|
131
|
+
if (Date.now() > deadline) throw err;
|
|
132
|
+
await sleep(300);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function relaunch() {
|
|
138
|
+
if (process.platform !== 'win32') return false;
|
|
139
|
+
// 关键:不能把派生本 helper 的 ELECTRON_RUN_AS_NODE 漏给重新拉起的桌面,
|
|
140
|
+
// 否则 electron 会被当成纯 node 跑、窗口起不来。
|
|
141
|
+
const env = { ...process.env };
|
|
142
|
+
delete env.ELECTRON_RUN_AS_NODE;
|
|
143
|
+
|
|
144
|
+
// 重启进程是 helper 的普通子进程(helper 自身已 detach 脱离 electron 树),Windows 下父进程
|
|
145
|
+
// 正常退出不会杀子进程,所以绝不能再加 detached:true——它会打断 cmd.exe→pnpm.cmd→node 的
|
|
146
|
+
// stdio 句柄继承,导致 pnpm 输出/执行丢失、桌面起不来。输出重定向到 relaunch.log 便于排查。
|
|
147
|
+
async function launch(cmd, args, opts = {}) {
|
|
148
|
+
const logPath = path.join(home, '.dsh-pack', 'relaunch.log');
|
|
149
|
+
await fsp.mkdir(path.dirname(logPath), { recursive: true });
|
|
150
|
+
const logFd = await fsp.open(logPath, 'a');
|
|
151
|
+
const child = spawn(cmd, args, {
|
|
152
|
+
...opts,
|
|
153
|
+
stdio: ['ignore', logFd.fd, logFd.fd],
|
|
154
|
+
windowsHide: true,
|
|
155
|
+
env,
|
|
156
|
+
});
|
|
157
|
+
child.unref();
|
|
158
|
+
// 宽限 3s:cmd.exe→pnpm.cmd→tsx→electron 链需要时间真正建立;父进程此刻退出不杀子进程,
|
|
159
|
+
// 但链尚未建立就退出会让重启中断,且 logFd 需等子进程继承完成后再关。
|
|
160
|
+
await sleep(3000);
|
|
161
|
+
await logFd.close().catch(() => {});
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (relaunchCmd) {
|
|
166
|
+
return await launch('cmd.exe', ['/d', '/s', '/c', relaunchCmd], { cwd: relaunchCwd || undefined });
|
|
167
|
+
}
|
|
168
|
+
const cmdline = await captureCommandLine(electronPid);
|
|
169
|
+
if (cmdline) {
|
|
170
|
+
return await launch(cmdline, [], { shell: true });
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// relaunch 的 pnpm→tsx→electron 链要 ~15s 才真正把桌面拉起来;若只按 relaunch 的 3s 宽限就
|
|
176
|
+
// 标「完成」,进度窗口关了桌面却还没起来,用户得干等十几秒。这里轮询等客户端真正可用。
|
|
177
|
+
//
|
|
178
|
+
// 判据:探测 web-app 的 HTTP 端口(127.0.0.1:19387,harness desktop-host 写死 --port 19387)。
|
|
179
|
+
// 只判断「electron 主进程存在」是不够的——主进程 1~3s 就 spawn 出来,但此刻窗口还是白屏、
|
|
180
|
+
// web-app 还没监听端口,标 done 仍偏早。端口能连上才说明 host 已就绪、窗口开始渲染。
|
|
181
|
+
const CLIENT_PORT = 19387;
|
|
182
|
+
async function checkClientUp() {
|
|
183
|
+
return new Promise((resolve) => {
|
|
184
|
+
const req = http.get({ host: '127.0.0.1', port: CLIENT_PORT, path: '/', timeout: 1500 }, (res) => {
|
|
185
|
+
res.resume();
|
|
186
|
+
resolve(true);
|
|
187
|
+
});
|
|
188
|
+
req.on('error', () => resolve(false));
|
|
189
|
+
req.on('timeout', () => { req.destroy(); resolve(false); });
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function waitForDesktopReady() {
|
|
194
|
+
if (process.platform !== 'win32') return;
|
|
195
|
+
const deadline = Date.now() + 25000;
|
|
196
|
+
while (Date.now() < deadline) {
|
|
197
|
+
if (await checkClientUp()) return;
|
|
198
|
+
await sleep(500);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function main() {
|
|
203
|
+
await log(`migrate start: target=${target} profiles=${profilesDir} electronPid=${electronPid} progressPid=${progressPid} relaunchCmd=${relaunchCmd} relaunchCwd=${relaunchCwd}`);
|
|
204
|
+
await sleep(2000); // 宽限:让宿主把「migrating」响应 flush 给 UI,再动手杀
|
|
205
|
+
await setStep(home, 'kill', 'running');
|
|
206
|
+
await killElectronTree(electronPid);
|
|
207
|
+
await setStep(home, 'kill', 'done');
|
|
208
|
+
await log('electron tree killed, waiting for lock release');
|
|
209
|
+
await sleep(300); // 让进度窗口看清「杀进程」完成
|
|
210
|
+
const r = await waitForLockRelease(); // 内部 onProgress 写 move/link
|
|
211
|
+
await log(`switch done: active=${r.active} method=${r.method}`);
|
|
212
|
+
await setStep(home, 'launch', 'running');
|
|
213
|
+
const ok = await relaunch();
|
|
214
|
+
if (ok) await waitForDesktopReady();
|
|
215
|
+
await setStep(home, 'launch', 'done');
|
|
216
|
+
await setPhase(home, 'done');
|
|
217
|
+
await log(`relaunch: ${ok ? 'spawned' : 'skipped'}`);
|
|
218
|
+
process.exit(0);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
main().catch(async (err) => {
|
|
222
|
+
await setPhase(home, 'failed', err?.message ?? String(err));
|
|
223
|
+
await log(`migrate FAILED: ${err?.stack ?? err}`);
|
|
224
|
+
process.exit(1);
|
|
225
|
+
});
|
package/src/migrate.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// 脱管迁移进程的宿主侧入口(机制 B 的「派生」半步)。
|
|
2
|
+
//
|
|
3
|
+
// 运行中的桌面持有 profiles/desktop 内句柄,首次切换要 rename(desktop→default) 必 EBUSY。
|
|
4
|
+
// 所以派生一个与宿主进程树脱钩的进程:杀 electron 释放锁 → 改名 → 建 junction → 重启桌面。
|
|
5
|
+
//
|
|
6
|
+
// 派生技巧:用 process.execPath(= electron.exe)+ ELECTRON_RUN_AS_NODE=1 当纯 node 跑 helper,
|
|
7
|
+
// 不依赖 PATH 上的 node(安装环境里 node 不一定在 PATH,但 electron 一定在)。
|
|
8
|
+
// detached:true 让 helper 挂在独立进程组,taskkill /T 杀 electron 树时杀不到它。
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
|
|
14
|
+
const HELPER = fileURLToPath(new URL('./migrate-helper.js', import.meta.url));
|
|
15
|
+
|
|
16
|
+
/** dev 沙盒路径 <repo>/apps/desktop/.desktop-build/... → 反推仓库根;非 dev 返回 null。 */
|
|
17
|
+
function detectDevRepoRoot(profilesDir) {
|
|
18
|
+
const parts = profilesDir.split(path.sep);
|
|
19
|
+
const i = parts.indexOf('.desktop-build');
|
|
20
|
+
if (i < 2) return null;
|
|
21
|
+
const root = parts.slice(0, i - 2).join(path.sep);
|
|
22
|
+
return root || null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 派生脱管迁移进程。宿主应立即把返回值(restarting)回给 UI,然后等待桌面被 helper 杀掉重启。
|
|
27
|
+
* @returns {Promise<number|undefined>} helper 的 pid
|
|
28
|
+
*/
|
|
29
|
+
export function spawnMigrationHelper(runtime, target, { progressPid } = {}) {
|
|
30
|
+
const electronPid = process.ppid; // 宿主进程的父 = electron 主进程(进程树已确证)
|
|
31
|
+
const args = [
|
|
32
|
+
HELPER,
|
|
33
|
+
`--electron-pid=${electronPid}`,
|
|
34
|
+
`--profiles-dir=${runtime.profilesDir}`,
|
|
35
|
+
`--home=${runtime.home}`,
|
|
36
|
+
`--target=${target}`,
|
|
37
|
+
];
|
|
38
|
+
if (progressPid) args.push(`--progress-pid=${progressPid}`);
|
|
39
|
+
const repoRoot = detectDevRepoRoot(runtime.profilesDir);
|
|
40
|
+
if (repoRoot) {
|
|
41
|
+
args.push(`--relaunch-cwd=${repoRoot}`);
|
|
42
|
+
args.push('--relaunch-cmd=pnpm start:desktop');
|
|
43
|
+
}
|
|
44
|
+
const child = spawn(process.execPath, args, {
|
|
45
|
+
detached: true,
|
|
46
|
+
stdio: 'ignore',
|
|
47
|
+
// 关键:不能用宿主进程的 cwd(可能是 profiles/desktop)——Windows 里进程 CWD 是打开句柄,
|
|
48
|
+
// 会让 helper 自己占着正要 rename 的目录,导致永远 EBUSY。放用户主目录,完全脱离 DSH 树。
|
|
49
|
+
cwd: os.homedir(),
|
|
50
|
+
env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
|
|
51
|
+
windowsHide: true,
|
|
52
|
+
});
|
|
53
|
+
child.on('error', () => {}); // 派生失败不炸宿主(由日志兜底)
|
|
54
|
+
child.unref();
|
|
55
|
+
return child.pid;
|
|
56
|
+
}
|
package/src/packaged.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// 打包态判定:决定「切换进度窗」(progress-window)走 electron GUI 还是 PowerShell WPF。
|
|
2
|
+
// 任务中心已改内嵌面板(settings.js),不再有独立窗口,故不参与此判定。
|
|
3
|
+
//
|
|
4
|
+
// 开发态 `electron .` 由默认 app 承载,process.defaultApp === true;打包后的 app 不是默认 app,
|
|
5
|
+
// 该值为 undefined。纯 node(测试/CLI)里则连 process.versions.electron 都没有。
|
|
6
|
+
// 三个状态唯一可区分的是「打包后的 electron app」:有 electron 版本号 + 不是默认 app。
|
|
7
|
+
export function isPackagedBuild() {
|
|
8
|
+
return typeof process.versions.electron === 'string' && process.defaultApp !== true;
|
|
9
|
+
}
|
package/src/profiles.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// 多 profile 目录管理(方案 B 的「安装进兄弟 profile」)。
|
|
2
|
+
//
|
|
3
|
+
// profile 目录是 DSH 布局的一部分,由本插件拥有;纯 node:fs,不依赖 core。
|
|
4
|
+
// 关键约定:
|
|
5
|
+
// - 真实 profile 各自占用 profiles/<name>/ 一个目录;
|
|
6
|
+
// - profiles/desktop 是官方硬编码的激活指针:切换后它永远是一个 junction,
|
|
7
|
+
// 指向「当前激活」的真实 profile 目录(见 junction.js)。
|
|
8
|
+
import fsp from 'node:fs/promises';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { PROFILES_DIR, ACTIVE_NAME } from './runtime.js';
|
|
11
|
+
import { RESERVED_PROFILE_NAMES } from './channel.js';
|
|
12
|
+
import { HOME_ARTIFACT_STORE } from './core/home-store.js';
|
|
13
|
+
import { skipProfileDir } from './core/discovery.js';
|
|
14
|
+
|
|
15
|
+
// home 级换指 slot 的 stash 根目录(与 home-store.js 里 storeHomeRel 的 .dsh-pack 前缀一致)。
|
|
16
|
+
const HOME_STASH = '.dsh-pack';
|
|
17
|
+
|
|
18
|
+
const STATE_FILE = 'dsh-packforge.json';
|
|
19
|
+
|
|
20
|
+
export function statePath(home) {
|
|
21
|
+
return path.join(home, STATE_FILE);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 读插件持久状态;不存在/损坏 → 空对象。 */
|
|
25
|
+
export async function readState(home) {
|
|
26
|
+
try {
|
|
27
|
+
const raw = await fsp.readFile(statePath(home), 'utf8');
|
|
28
|
+
return JSON.parse(raw);
|
|
29
|
+
} catch {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function writeState(home, state) {
|
|
35
|
+
await fsp.mkdir(home, { recursive: true });
|
|
36
|
+
await fsp.writeFile(statePath(home), JSON.stringify(state, null, 2), 'utf8');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function lstat(p) {
|
|
40
|
+
try {
|
|
41
|
+
return await fsp.lstat(p);
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 当前激活的 profile 逻辑名:desktop 是 junction → 目标目录名;真实目录 → 'default'。 */
|
|
48
|
+
export async function resolveActiveName(runtime) {
|
|
49
|
+
const desktop = path.join(runtime.profilesDir, ACTIVE_NAME);
|
|
50
|
+
const st = await lstat(desktop);
|
|
51
|
+
if (!st) return null;
|
|
52
|
+
if (st.isSymbolicLink()) {
|
|
53
|
+
const target = await fsp.readlink(desktop);
|
|
54
|
+
return path.basename(path.resolve(runtime.profilesDir, target));
|
|
55
|
+
}
|
|
56
|
+
return 'default';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 列出 profiles/<name>/ 下全部逻辑 profile(去重,desktop 指针本身不算一个 profile)。 */
|
|
60
|
+
export async function listProfiles(runtime) {
|
|
61
|
+
const { profilesDir } = runtime;
|
|
62
|
+
const activeName = await resolveActiveName(runtime);
|
|
63
|
+
const entries = [];
|
|
64
|
+
try {
|
|
65
|
+
for (const d of await fsp.readdir(profilesDir, { withFileTypes: true })) {
|
|
66
|
+
if (d.name === ACTIVE_NAME) continue; // 指针占位,不单独列出
|
|
67
|
+
if (!d.isDirectory()) continue;
|
|
68
|
+
if (skipProfileDir(d.name)) continue; // node_modules / __temp__ / 点目录不是整合包
|
|
69
|
+
const abs = path.join(profilesDir, d.name);
|
|
70
|
+
entries.push({ name: d.name, dir: abs, active: d.name === activeName, junction: false });
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
// profiles 目录尚未存在 → 空列表
|
|
74
|
+
}
|
|
75
|
+
// 首次切换前 desktop 仍是真实目录:补一条「default」逻辑 profile。
|
|
76
|
+
const desktop = path.join(profilesDir, ACTIVE_NAME);
|
|
77
|
+
const st = await lstat(desktop);
|
|
78
|
+
if (st && st.isDirectory() && !st.isSymbolicLink()) {
|
|
79
|
+
entries.unshift({ name: 'default', dir: desktop, active: activeName === 'default', junction: false });
|
|
80
|
+
}
|
|
81
|
+
return entries;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function createProfile(home, name) {
|
|
85
|
+
if (RESERVED_PROFILE_NAMES.includes(name)) {
|
|
86
|
+
throw new Error(`「${name}」是保留名,不能作为 profile 名`);
|
|
87
|
+
}
|
|
88
|
+
const dir = path.join(home, PROFILES_DIR, name);
|
|
89
|
+
await fsp.mkdir(dir, { recursive: true });
|
|
90
|
+
return dir;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function deleteProfile(home, name) {
|
|
94
|
+
// desktop 指针、default 存档、以及当前激活的真实 profile 都不可删,
|
|
95
|
+
// 否则 desktop / skills / .agent-presets 会成悬空指针。
|
|
96
|
+
const active = await resolveActiveName({ profilesDir: path.join(home, PROFILES_DIR) });
|
|
97
|
+
if (name === ACTIVE_NAME || name === 'default' || name === active) {
|
|
98
|
+
throw new Error(`「${name}」是当前激活的 profile,不能删除`);
|
|
99
|
+
}
|
|
100
|
+
await fsp.rm(path.join(home, PROFILES_DIR, name), { recursive: true, force: true });
|
|
101
|
+
// skills / .agent-presets 的真实数据在 .dsh-pack/<store>/<name>/(换指 slot),删 profile 时一并清除,
|
|
102
|
+
// 否则留在 home 里成孤儿(desktop 指针已指向别的 profile,谁也读不到它们)。
|
|
103
|
+
for (const store of Object.values(HOME_ARTIFACT_STORE)) {
|
|
104
|
+
await fsp.rm(path.join(home, HOME_STASH, store, name), { recursive: true, force: true });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// 切换进度窗口(方案 A 执行体)。
|
|
2
|
+
//
|
|
3
|
+
// 由 progress.js 用 process.execPath 直接派生、不带 ELECTRON_RUN_AS_NODE,走 GUI:
|
|
4
|
+
// frameless + always-on-top 小窗,主进程轮询 progress.json,phase=done/failed 时自动关闭。
|
|
5
|
+
// 渲染在 progress.html(renderer 直读 progress.json,走 nodeIntegration,无远程内容、无需 IPC)。
|
|
6
|
+
const { app, BrowserWindow } = require('electron');
|
|
7
|
+
const fs = require('node:fs');
|
|
8
|
+
const path = require('node:path');
|
|
9
|
+
|
|
10
|
+
const progressFile = (() => {
|
|
11
|
+
for (const a of process.argv.slice(2)) {
|
|
12
|
+
const m = /^--progress-file=(.*)$/.exec(a);
|
|
13
|
+
if (m) return m[1];
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
})();
|
|
17
|
+
|
|
18
|
+
let win = null;
|
|
19
|
+
let closed = false;
|
|
20
|
+
|
|
21
|
+
function closeAfter(ms) {
|
|
22
|
+
if (closed) return;
|
|
23
|
+
closed = true;
|
|
24
|
+
setTimeout(() => {
|
|
25
|
+
if (win && !win.isDestroyed()) win.close();
|
|
26
|
+
}, ms);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function tick() {
|
|
30
|
+
if (!win || win.isDestroyed()) return;
|
|
31
|
+
let raw = null;
|
|
32
|
+
try {
|
|
33
|
+
raw = fs.readFileSync(progressFile, 'utf8');
|
|
34
|
+
} catch { /* 尚未写入 */ }
|
|
35
|
+
if (!raw) return;
|
|
36
|
+
let data = null;
|
|
37
|
+
try {
|
|
38
|
+
data = JSON.parse(raw);
|
|
39
|
+
} catch { return; /* 半截写,等下一帧 */ }
|
|
40
|
+
if (data.phase === 'done') closeAfter(1200);
|
|
41
|
+
else if (data.phase === 'failed') closeAfter(2500);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!progressFile) {
|
|
45
|
+
app.whenReady().then(() => app.quit());
|
|
46
|
+
} else {
|
|
47
|
+
app.whenReady().then(() => {
|
|
48
|
+
win = new BrowserWindow({
|
|
49
|
+
width: 420,
|
|
50
|
+
height: 380,
|
|
51
|
+
frame: false,
|
|
52
|
+
resizable: false,
|
|
53
|
+
alwaysOnTop: true,
|
|
54
|
+
skipTaskbar: true,
|
|
55
|
+
show: false,
|
|
56
|
+
backgroundColor: '#1e1f24',
|
|
57
|
+
webPreferences: {
|
|
58
|
+
nodeIntegration: true,
|
|
59
|
+
contextIsolation: false,
|
|
60
|
+
sandbox: false,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
win.loadFile(path.join(__dirname, 'progress.html'), { query: { file: progressFile } });
|
|
64
|
+
win.once('ready-to-show', () => {
|
|
65
|
+
if (win && !win.isDestroyed()) win.show();
|
|
66
|
+
});
|
|
67
|
+
const timer = setInterval(tick, 200);
|
|
68
|
+
win.on('closed', () => {
|
|
69
|
+
clearInterval(timer);
|
|
70
|
+
win = null;
|
|
71
|
+
});
|
|
72
|
+
// 兜底:60s 无论如何都关(helper 若中途崩掉,窗口不能赖在屏幕上)。
|
|
73
|
+
setTimeout(() => {
|
|
74
|
+
if (win && !win.isDestroyed()) win.close();
|
|
75
|
+
}, 60000);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
app.on('window-all-closed', () => app.quit());
|