glasspane-install 1.25.2 → 1.26.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.
Files changed (4) hide show
  1. package/README.md +5 -1
  2. package/cli.js +128 -21
  3. package/glasspane.js +89 -1
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -17,7 +17,11 @@ npx glasspane-install
17
17
 
18
18
  # 2) Fetch, then run: install.sh clones the pinned release tag. Two steps on
19
19
  # purpose — piped into `sh`, a curl that fails leaves stdin empty and the pipeline exits 0.
20
- curl -fsSL https://raw.githubusercontent.com/jingzhao-l/GlassPane/main/install.sh -o /tmp/gp-install.sh && sh /tmp/gp-install.sh
20
+ # The landing file comes from `mktemp`: `/tmp` is mode 1777, and the sticky bit only stops
21
+ # another account from deleting *your* file — it does not stop them from creating
22
+ # `/tmp/gp-install.sh -> ~/.bashrc` first, and `curl -o` follows that link and truncates
23
+ # the target as *you*.
24
+ GP="$(mktemp "${TMPDIR:-/tmp/}gp-install.XXXXXXXX")" && curl -fsSL https://raw.githubusercontent.com/jingzhao-l/GlassPane/main/install.sh -o "$GP" && sh "$GP" && rm -f "$GP"
21
25
 
22
26
  # 3) From an existing checkout
23
27
  node installer/cli.js
package/cli.js CHANGED
@@ -70,7 +70,7 @@ export const INSTALL_SH_URL =
70
70
  /** 发布线锚点(版本真源 = 根 package.json,由 scripts/set-version.mjs 统一改写,
71
71
  * 勿手改):npx 形态下本地没有仓库时,引导 clone 的就是这个 tag,与 install.sh
72
72
  * 的 `GLASSPANE_RELEASE` 同值——两条一键入口必须拿到同一份源码。 */
73
- export const RELEASE_VERSION = '1.25.2'
73
+ export const RELEASE_VERSION = '1.26.0'
74
74
  /** 发布 ref(tag 名)。GLASSPANE_REF 环境变量可覆盖(追主干用 `main`)。 */
75
75
  export const REPO_REF = `v${RELEASE_VERSION}`
76
76
 
@@ -238,13 +238,26 @@ export function parseArgs(argv) {
238
238
  return { options: opts, error: null }
239
239
  }
240
240
 
241
+ /** "这一棵是不是 GlassPane 仓库"的**唯一一份**判据(两个 marker 的相对路径)。
242
+ * install.sh 的 `looks_like_repo` 测的就是这两个路径,两处由
243
+ * `installer/test/daily-20261029-installer-lane7.test.mjs` 逐值对照——在那之前"两条入口的判据
244
+ * 同源"只是两句注释互相指认,脚本里那份是没人对照的第二份抄本。 */
245
+ export const REPO_MARKERS = Object.freeze(['engine/Package.swift', 'mcp-shell/package.json'])
246
+
247
+ /** 只问**这一层目录**,一律不向上走:`looksLikeRepoHere(<外层repo>/vendored)` 答 false,
248
+ * 而 {@link resolveProjectRoot} 会答出外层那一棵。S7-1(2026-10-29,P0)修的就是这个差别——
249
+ * 判断"目标是不是仓库"时向上走,别人的仓库子目录就被读成"既有 clone",随后 fetch/checkout
250
+ * 动的是外层那棵树。 */
251
+ export function looksLikeRepoHere(dir) {
252
+ if (typeof dir !== 'string' || dir === '') return false
253
+ return REPO_MARKERS.every((rel) => fs.existsSync(path.join(dir, rel)))
254
+ }
255
+
241
256
  /** 从起始目录向上逐级寻找含 engine/Package.swift 与 mcp-shell/package.json 的根目录。 */
242
257
  export function resolveProjectRoot(startDir = process.cwd()) {
243
258
  let dir = path.resolve(startDir)
244
259
  while (true) {
245
- const hasEngine = fs.existsSync(path.join(dir, 'engine', 'Package.swift'))
246
- const hasShell = fs.existsSync(path.join(dir, 'mcp-shell', 'package.json'))
247
- if (hasEngine && hasShell) {
260
+ if (looksLikeRepoHere(dir)) {
248
261
  return dir
249
262
  }
250
263
  const parent = path.dirname(dir)
@@ -2239,8 +2252,10 @@ export function usageText() {
2239
2252
  ' 注:install.sh 用 exec 委托本文件,所以**脚本自己跑完了**之后,退出码原样透传。',
2240
2253
  ' 但 `curl … | sh` 的 $? 只在 curl 成功时才是这里的结论:curl 取不到脚本时 sh 读到的',
2241
2254
  ' 是空输入,直接 EOF 退 0(本机实测 curl 退 7、管道退 0);中途断流时 sh 只执行了',
2242
- ' 收到的前半截,回的是最后一条命令的状态。要拿退出码当结论就分两步跑:',
2243
- ' curl -fsSL <install.sh> -o /tmp/gp-install.sh && sh /tmp/gp-install.sh',
2255
+ ' 收到的前半截,回的是最后一条命令的状态。要拿退出码当结论就分两步跑,且落点必须是',
2256
+ ' 一次性文件(`/tmp` 是 1777:sticky 只挡住别人删你的目录项,挡不住他先建一个',
2257
+ ' `/tmp/gp-install.sh -> ~/.bashrc`,而 curl -o 会跟随那个符号链接、以你的 uid 截断覆写它):',
2258
+ ' GP="$(mktemp "${TMPDIR:-/tmp/}gp-install.XXXXXXXX")" && curl -fsSL <install.sh> -o "$GP" && sh "$GP" && rm -f "$GP"',
2244
2259
  '',
2245
2260
  ].join('\n')
2246
2261
  }
@@ -2504,8 +2519,8 @@ export function writePrivatePlist(plistPath, xml, {
2504
2519
 
2505
2520
  /** clone 失败的归因(finding 9):把 **git 自己的原话**交给读者,而不是把任何非零退出
2506
2521
  * 都算成"网络不可达或该发布 tag 不存在"。"destination path … already exists and is not an
2507
- * empty directory" 是真能走到的(bootstrapPlan 只拒绝"存在且父目录也不是仓库"的那种),
2508
- * 被归因成网络问题时读者会一直重试一条永远不通的路。 */
2522
+ * empty directory" 是真能走到的(bootstrapPlan 只拒绝"这一层目录自己不是 GlassPane 仓库"的那种,
2523
+ * 判据见 looksLikeRepoHere),被归因成网络问题时读者会一直重试一条永远不通的路。 */
2509
2524
  export function cloneFailureText({ ref, status, stderr = '', targetDir }) {
2510
2525
  const detail = String(stderr ?? '').trim()
2511
2526
  const head = `git clone 失败(ref '${ref}',目标 ${targetDir},退出码 ${status ?? 'null'})。`
@@ -2550,7 +2565,11 @@ export function bootstrapPlan({
2550
2565
  return { error: '无法确定用户主目录(HOME 为空),不知道把源码 clone 到哪里' }
2551
2566
  }
2552
2567
  const targetDir = installDir || path.join(homeDir, 'glasspane')
2553
- if (fs.existsSync(targetDir) && resolveProjectRoot(targetDir) === null) {
2568
+ // 只问这一层,不向上走(S7-1,2026-10-29 P0)。这里从前用的是 `resolveProjectRoot(targetDir)`,
2569
+ // 而它会一路向上找到外层的仓库:`<外层repo>/vendored` 于是"是一个仓库",躲过这条拒绝,
2570
+ // 后面的 fetch/checkout 就全落在外层那一棵上(同一条输入 install.sh 的答案是"目标目录已存在
2571
+ // 且不是 GlassPane 仓库"+ rc 1——两条一键入口对着同一个目录给两种结论,修的就是这一类)。
2572
+ if (fs.existsSync(targetDir) && !looksLikeRepoHere(targetDir)) {
2554
2573
  return {
2555
2574
  error: `目标目录已存在且不是 GlassPane 仓库:${targetDir}(用 --repo 指定仓库,或清理该目录后重试)`,
2556
2575
  }
@@ -2578,6 +2597,59 @@ export function bootstrapPlan({
2578
2597
  }
2579
2598
  }
2580
2599
 
2600
+ /**
2601
+ * 两条路径是不是同一个东西。`git rev-parse --show-toplevel` 打的是**物理路径**(macOS 上
2602
+ * `/var` → `/private/var`、家目录里的软链都会被解开),所以两侧都要过一遍 realpath 才可比;
2603
+ * 路径不存在时 realpathSync 抛,退回 `path.resolve`——不存在的那一侧本来就答不出仓库根。
2604
+ */
2605
+ function canonicalPath(p) {
2606
+ try {
2607
+ return fs.realpathSync(p)
2608
+ } catch {
2609
+ return path.resolve(p)
2610
+ }
2611
+ }
2612
+
2613
+ /** 问一次「这一带最近的仓库根在哪」,把答案作为数据交回去(不判定)。 */
2614
+ export function readRepoToplevel({ dir, spawn = spawnSync } = {}) {
2615
+ const res = spawn('git', ['-C', dir, 'rev-parse', '--show-toplevel'], { encoding: 'utf8' })
2616
+ const answer = String(res?.stdout ?? '').trim()
2617
+ if (res?.status !== 0 || answer === '') {
2618
+ return { read: false, toplevel: null }
2619
+ }
2620
+ return { read: true, toplevel: answer }
2621
+ }
2622
+
2623
+ /**
2624
+ * 纯判定:要动手的那一棵,是不是说话人指的那一棵?(S7-1,2026-10-29,P0)
2625
+ * `git -C <dir>` 对**任何**目录都按"离它最近的那棵树"答话,所以 `git -C <外层repo>/vendored
2626
+ * status` 与 `checkout --detach` 动的都是外层那棵树——本轮实测把外层 main detach 了。
2627
+ * 只答两种"不是同一棵":答不出(`unreadable`,那目录不是任何仓库的一部分,或 git 没答话),
2628
+ * 与答的是别处(`outside`)。相等就回 null。
2629
+ */
2630
+ export function toplevelProblemFor({ targetDir, toplevel = null, readOk = false } = {}) {
2631
+ if (!readOk) return { kind: 'unreadable' }
2632
+ if (canonicalPath(toplevel) === canonicalPath(targetDir)) return null
2633
+ return { kind: 'outside', toplevel }
2634
+ }
2635
+
2636
+ /** 两种"不是同一棵"各说一句;两处调用(纯判据、IO 步)共用这一份,不发明第二套说法。 */
2637
+ export function toplevelRefuseText({ targetDir, problem }) {
2638
+ if (problem?.kind === 'outside') {
2639
+ return `${targetDir} 自己不是一棵 git 仓库的根:git 说这一带最近的根在 **${problem.toplevel}**。`
2640
+ + `\n本轮要是动手,动的就是 ${problem.toplevel} 那一棵,而不是你指的 ${targetDir}——那是别人的仓库,`
2641
+ + '安装器不在它上面 fetch、不切它的分支、不碰它的工作树。'
2642
+ + `\n下一步(两条都做得动,且不销毁任何东西):`
2643
+ + `\n a) 换一处不属于任何仓库的落点:GLASSPANE_INSTALL_DIR=<空目录> 重跑一键安装`
2644
+ + '(那个目录自己要能让 git -C <目录> rev-parse --show-toplevel 答不出才算它自己);'
2645
+ + `\n b) 要装的那一棵在别处就用 --repo / GLASSPANE_REPO 指过去,指的目录必须自己就是仓库根。`
2646
+ }
2647
+ return `${targetDir} 答不出 git 仓库根(这一带它不是 git 仓库,或 git 本身没答话):`
2648
+ + '连"要动的是哪一棵"都判不出来,就不动它,也不装它。'
2649
+ + `\n下一步:确认 ${targetDir} 是一棵 git 仓库(git -C "${targetDir}" rev-parse --show-toplevel 能不能答话);`
2650
+ + '要一份全新的 clone 就 GLASSPANE_INSTALL_DIR=<空目录> 重跑一键安装。'
2651
+ }
2652
+
2581
2653
  /**
2582
2654
  * 既有 clone 的结论(纯判定,可单测)。
2583
2655
  *
@@ -2588,6 +2660,9 @@ export function bootstrapPlan({
2588
2660
  * "destination path … already exists and is not an empty directory" 拒掉,读者只剩"换目录"这一条死路。
2589
2661
  *
2590
2662
  * 判据按那个人 2026-10-26 选的形状:
2663
+ * · 先问「要动的是不是这一棵」(`toplevelProblem`,见 `readRepoToplevel`):答不出或答的是
2664
+ * 别处 ⇒ 拒绝,点名要动的到底是哪一棵——这一问排在脏/干净之前,因为"这棵树干净"是对着
2665
+ * 别人的仓库说的结论(S7-1,2026-10-29 P0);
2591
2666
  * · 干净(`git status --porcelain` 空)且 HEAD 已经是那个 tag ⇒ 就用;
2592
2667
  * · 干净但不是那个 tag ⇒ 前进(fetch + detached checkout —— 这是移动**产品自己的 clone**,
2593
2668
  * 不是移动任何人的工作树);
@@ -2602,7 +2677,17 @@ export function bootstrapPlan({
2602
2677
  * 现在 tag 缺席归到"前进"里:fetch 不成仍由 `advanceExistingClone` 拒,并交出 git 的原话;
2603
2678
  * HEAD 读不出照旧是拒。前者是能补的事实缺口,后者是判据本身没了。
2604
2679
  */
2605
- export function existingCloneDecision({ targetDir, ref, head = null, tagCommit = null, blocking = [] }) {
2680
+ export function existingCloneDecision({ targetDir, ref, head = null, tagCommit = null, blocking = [], toplevelProblem = null }) {
2681
+ // 「要动的是哪一棵」问在「这棵树脏不脏」之前:判不出对象的时候,后面每一条结论都是对着
2682
+ // 别人的仓库说的(S7-1 的实测形状:外层 main 被 detach,然后回一句 advance: use)。
2683
+ if (toplevelProblem) {
2684
+ return {
2685
+ action: 'refuse',
2686
+ blocking: [],
2687
+ toplevelProblem,
2688
+ text: toplevelRefuseText({ targetDir, problem: toplevelProblem }),
2689
+ }
2690
+ }
2606
2691
  const dirty = blocking.map((line) => String(line).trim()).filter((line) => line !== '')
2607
2692
  if (dirty.length > 0) {
2608
2693
  const shown = dirty.slice(0, 10)
@@ -2663,6 +2748,9 @@ export function existingCloneDecision({ targetDir, ref, head = null, tagCommit =
2663
2748
  * `git status --porcelain` 的行,以及这份 clone 是不是浅仓。判据一律在 `existingCloneDecision`,
2664
2749
  * 两处调用(bootstrap 的纯计划、前进这一步的复判)共用同一份读数,避免出现第二个"什么算脏"的说法。
2665
2750
  *
2751
+ * 这四份事实之前还有一问:`rev-parse --show-toplevel`——它决定"这四份事实到底是谁的事实"。
2752
+ * 答不出或答的是别处时**一份都不给**(见上面的 S7-1 注释),调用方拿到的就是 head=null。
2753
+ *
2666
2754
  * `shallow` 是给 fetch 挑形状的:`--depth 1` 会把一棵**完整**的 clone 变成浅仓(`.git/shallow`
2667
2755
  * 一旦落下,之后的遍历都撞上人为边界,要恢复得 `git fetch --unshallow`)。旧写法对任何树都带
2668
2756
  * `--depth 1`,而注释写的是"只往 .git 里加对象"——那句话对完整 clone 不成立,它加的是一个边界。
@@ -2670,6 +2758,15 @@ export function existingCloneDecision({ targetDir, ref, head = null, tagCommit =
2670
2758
  * 一点也不给人家的历史加边界。
2671
2759
  */
2672
2760
  export function readCloneFacts({ targetDir, ref, spawn = spawnSync, fetchHead = false } = {}) {
2761
+ // 开头先问「要动的是不是这一棵」(S7-1)。`git -C <dir>` 对别人仓库的子目录一样答话,
2762
+ // 所以后面的 status / rev-parse 读回来的其实是**外层那棵树**的事实:外层干净、外层 HEAD 在
2763
+ // 某一笔上,于是这一棵被读成"干净的既有 clone",下一步 fetch/checkout 就 detach 了外层的 main。
2764
+ // 答不出或答的是别处 ⇒ 一条事实也不给(head 为 null),判据那一侧只能落到"拒"。
2765
+ const top = readRepoToplevel({ dir: targetDir, spawn })
2766
+ const toplevelProblem = toplevelProblemFor({ targetDir, toplevel: top.toplevel, readOk: top.read })
2767
+ if (toplevelProblem) {
2768
+ return { head: null, tagCommit: null, blocking: [], shallow: null, toplevelProblem, toplevel: top.toplevel }
2769
+ }
2673
2770
  const read = (args) => spawn('git', ['-C', targetDir, ...args], { encoding: 'utf8' })
2674
2771
  const sha = (what) => {
2675
2772
  const res = read(['rev-parse', '--verify', what])
@@ -2722,16 +2819,8 @@ export function resolveRemoteName({ remotes = [] } = {}) {
2722
2819
  /**
2723
2820
  * 把既有 clone 前进到钉住的 ref(IO 步;判据全在 `existingCloneDecision` 里)。
2724
2821
  *
2725
- * 只有这几种 git 动作:`status --porcelain`、两次 `rev-parse --verify`、`rev-parse
2726
- * --is-shallow-repository`(全是读),`fetch --tags [origin <ref>]`(只往 .git 里加对象;
2727
- * 带不带 `--depth 1` 由 git 自己答的那句"是不是浅仓"决定,见 `readCloneFacts`),以及
2728
- * `checkout --detach <ref>`(只在树干净时执行)。没有 `-f`、没有 `stash`、没有 `clean`、
2729
- * 没有 `reset --hard`:一个 installer 不该有能把别人的未提交工作抹掉的那一格。
2730
- */
2731
- /**
2732
- * 把既有 clone 前进到钉住的 ref(IO 步;判据全在 `existingCloneDecision` 里)。
2733
- *
2734
- * 只有这几种 git 动作:`status --porcelain`、`remote`、几次 `rev-parse --verify`、`rev-parse
2822
+ * 只有这几种 git 动作:开头一次 `rev-parse --show-toplevel`(问"要动的是不是这一棵",见下面的
2823
+ * S7-1 注释)、`status --porcelain`、`remote`、几次 `rev-parse --verify`、`rev-parse
2735
2824
  * --is-shallow-repository`(全是读),`fetch --tags [remote <ref>]`(只往 .git 里加对象;
2736
2825
  * 带不带 `--depth 1` 由 git 自己答的那句"是不是浅仓"决定,见 `readCloneFacts`),以及
2737
2826
  * `checkout --detach <那个 commit 的 sha>`(只在树干净时执行)。没有 `-f`、没有 `stash`、没有
@@ -2742,6 +2831,22 @@ export function resolveRemoteName({ remotes = [] } = {}) {
2742
2831
  * 拒绝;而那次 fetch 的落点(FETCH_HEAD)就是一个 sha,切它才是这一句本来的意思。
2743
2832
  */
2744
2833
  export function advanceExistingClone({ repoDir, ref, spawn = spawnSync } = {}) {
2834
+ // 第一件事不是"这棵树脏不脏",而是"要动的是不是这一棵"(S7-1,2026-10-29 P0):
2835
+ // `git -C <dir> …` 会落到离它最近的那棵树,所以从前这一支对着 `<外层repo>/vendored` 跑
2836
+ // `checkout --detach` 时,切走的是**外层的 main**(本轮实测:detach 完还回一句 advance: use)。
2837
+ // 这一问在这里独立跑一次,不靠 `readCloneFacts` 内部那道——两个函数都可能被单独调用,
2838
+ // 守卫住在各自开头才算各自守得住。
2839
+ const top = readRepoToplevel({ dir: repoDir, spawn })
2840
+ const toplevelProblem = toplevelProblemFor({ targetDir: repoDir, toplevel: top.toplevel, readOk: top.read })
2841
+ if (toplevelProblem) {
2842
+ return {
2843
+ action: 'refuse',
2844
+ blocking: [],
2845
+ toplevelProblem,
2846
+ toplevel: top.toplevel,
2847
+ text: toplevelRefuseText({ targetDir: repoDir, problem: toplevelProblem }),
2848
+ }
2849
+ }
2745
2850
  const read = (args) => spawn('git', ['-C', repoDir, ...args], { encoding: 'utf8' })
2746
2851
  const remoteNames = () => {
2747
2852
  const res = read(['remote'])
@@ -3066,7 +3171,9 @@ export async function install({
3066
3171
  })}\n\n${repoMissingText()}`,
3067
3172
  )
3068
3173
  }
3069
- if (resolveProjectRoot(plan.targetDir) === null) {
3174
+ // 核对的也是**这一层**:clone 出来的目录自己得有 marker。从前这里用 resolveProjectRoot,
3175
+ // 它会向上走,于是一棵内容不完整的 clone 只要落在某个仓库里就能被判成"结构完整"。
3176
+ if (!looksLikeRepoHere(plan.targetDir)) {
3070
3177
  throw new Error(`clone 完成但目录结构不完整:${plan.targetDir}\n\n${repoMissingText()}`)
3071
3178
  }
3072
3179
  rootDir = plan.targetDir
package/glasspane.js CHANGED
@@ -37,9 +37,95 @@ export const VERBS = Object.freeze(['update', 'doctor', 'status', 'help'])
37
37
  export const VERB_ALIASES = Object.freeze({ '-h': 'help', '--help': 'help' })
38
38
  export const DAEMON_JOB_LABEL = 'com.glasspane.daemon'
39
39
  export const NPM_PACKAGES = Object.freeze(['glasspane-mcp', 'glasspane-install'])
40
+ /**
41
+ * The one line the npm remedy prints. It carries **no version placeholder on purpose**: the previous
42
+ * shape pinned each package to a version written inside angle brackets, and that opening bracket is a
43
+ * shell redirection — `/bin/sh -c '<那整行>'` fails with a syntax error (measured 2026-10-29, exit 2),
44
+ * so an agent copying the remedy could not even start the command. No version means "the registry's
45
+ * latest", which is what a missing package wants; pinning is a separate, explicit step
46
+ * (`npm view <pkg> versions`), and the remedy says so in prose instead of shipping an uncopyable token.
47
+ * Named and exported so a test can assert that *what is printed* is *what runs* — never a second copy
48
+ * of the command living in a template string.
49
+ */
50
+ export const NPM_GLOBAL_INSTALL_CMD = `npm install -g ${NPM_PACKAGES.join(' ')}`
40
51
  /** The three answers a read may give. `unverified` is the one that used to be missing everywhere. */
41
52
  export const READS = Object.freeze({ available: 'available', absent: 'absent', unverified: 'unverified' })
42
53
 
54
+ /** 要印进"可照抄的一行"里的原文,先剥掉 shell 元字符:npm 自己的原话带着 `'`、`<`、`|` 时,
55
+ * 那句话就不再能被 /bin/sh 解析了(S7-5 测的正是这一族)。裁的是元字符,不是信息。 */
56
+ function shellSafe(text) {
57
+ return String(text ?? '').replace(/[\n\r]/g, ' ').replace(/[<>&|;'"`\\$()]/g, '')
58
+ }
59
+
60
+ /**
61
+ * Is this machine's global npm prefix writable by *this* user? Three answers, same rule as READS:
62
+ * the claim printed in the remedy ("不需要权限") used to be backed by no probe at all — neither
63
+ * `npm prefix -g` nor a W_OK check happened anywhere in this file (or in the whole installer shard).
64
+ * `unverified` is therefore an answer of its own: "I could not look" is not "you do not need permission",
65
+ * and it is certainly not an escalation command.
66
+ */
67
+ export function probeGlobalNpmPrefix({
68
+ spawn = spawnSync,
69
+ exists = (p) => fs.existsSync(p),
70
+ writable = (p) => {
71
+ try {
72
+ fs.accessSync(p, fs.constants.W_OK)
73
+ return true
74
+ } catch {
75
+ return false
76
+ }
77
+ },
78
+ } = {}) {
79
+ const res = spawn('npm', ['prefix', '-g'], { encoding: 'utf8', timeout: 30_000 })
80
+ const status = typeof res?.status === 'number' ? res.status : null
81
+ if (status !== 0) {
82
+ return {
83
+ read: READS.unverified,
84
+ prefix: null,
85
+ target: null,
86
+ writable: null,
87
+ reason: status === null
88
+ ? `npm prefix -g 没答话(${res?.error?.message ?? String(res?.error ?? '没有退出码')})`
89
+ : `npm prefix -g 退出 ${status}`,
90
+ }
91
+ }
92
+ const prefix = String(res.stdout ?? '').trim()
93
+ if (prefix === '') return { read: READS.unverified, prefix: null, target: null, writable: null, reason: 'npm prefix -g 退 0 而答案是空的' }
94
+ // npm 真正落包的是 `<prefix>/lib/node_modules`;那一级不存在时退到 `lib`、再退到前缀本身——
95
+ // 要判的是"这一趟能不能写进去",所以问的是盘上真在的那一级。
96
+ const target = [path.join(prefix, 'lib', 'node_modules'), path.join(prefix, 'lib'), prefix].find((p) => exists(p)) ?? null
97
+ if (target === null) {
98
+ return { read: READS.unverified, prefix, target: null, writable: null, reason: `全局前缀 ${prefix} 在盘上不存在(lib/node_modules、lib、前缀本身三级都没读到目录)` }
99
+ }
100
+ return { read: READS.available, prefix, target, writable: writable(target) === true, reason: null }
101
+ }
102
+
103
+ /**
104
+ * The remedy line, composed from the probe's answer (pure: the three shapes are unit-testable without
105
+ * touching a machine). Never an escalation: when the prefix is not ours to write, the sentence names the
106
+ * prefix and the directory that refused W_OK and points at moving the prefix under the user — it does
107
+ * not hand out a privileged command (SECURITY.md §3, and this file's own header).
108
+ */
109
+ export function npmGlobalRemedy({ missing = [], prefixState } = {}) {
110
+ const named = missing.map((name) => shellSafe(name)).join(' ')
111
+ const cmd = NPM_GLOBAL_INSTALL_CMD
112
+ if (!prefixState || prefixState.read !== READS.available) {
113
+ const why = prefixState?.reason ?? '这一问没有答案'
114
+ return `补法(全局 npm 前缀的可写性**未验证**——${shellSafe(why)};这一问没答案,所以这一条不替你保证免权限):`
115
+ + `先让 npm prefix -g 能答话,再照这一行装 ${cmd}。不带版本号=取 registry 最新发布物,`
116
+ + `要钉版本先跑 npm view glasspane-mcp versions。缺的是 ${named || '(没读出来)'}。`
117
+ }
118
+ if (prefixState.writable === true) {
119
+ return `补法(不需要权限——本机探测过:全局前缀 ${shellSafe(prefixState.target)} 对本账号可写 W_OK):${cmd}。`
120
+ + `不带版本号=取 registry 最新发布物,要钉版本先跑 npm view glasspane-mcp versions。缺的是 ${named}。`
121
+ }
122
+ return `补法(**不给升权命令**:这台机器的全局 npm 前缀不可写——前缀 ${shellSafe(prefixState.prefix)},`
123
+ + `探测的是 ${shellSafe(prefixState.target)},它对 W_OK 答"不可写")。装进系统前缀不是这一层能替你做的事。`
124
+ + `下一步:先 npm config set prefix 指到一个你自己可写的目录(或用一个自带前缀的 Node 版本,nvm 就是这一档),`
125
+ + `再跑 ${cmd}(不带版本号=取 registry 最新发布物,要钉版本先跑 npm view glasspane-mcp versions)。缺的是 ${named}。`
126
+ }
127
+
128
+
43
129
  const POINTER_NAME = 'update-install.json'
44
130
  const STATE_NAME = 'update-state.json'
45
131
 
@@ -281,12 +367,14 @@ export function doctorReadings({
281
367
  const deps = npm.json.dependencies ?? {}
282
368
  const seen = NPM_PACKAGES.map((name) => `${name}=${deps[name]?.version ?? '没装'}`)
283
369
  const missing = NPM_PACKAGES.filter((name) => !deps[name])
370
+ // 缺包时要不要说"不需要权限",由这台机器答,不由这句话答(S7-5):先探一次全局前缀。
371
+ // 探测排在读完全局包之后、只在真要给出补法时才问——都在位时没人需要这一行。
284
372
  out.push({
285
373
  name: '全局 npm 包',
286
374
  read: missing.length === 0 ? READS.available : READS.absent,
287
375
  detail: seen.join(' '),
288
376
  remedy: missing.length === 0 ? null
289
- : `补法(不需要权限,取的是 registry 的发布物):npm install -g ${NPM_PACKAGES.map((n) => `${n}@<版本>`).join(' ')}`,
377
+ : npmGlobalRemedy({ missing, prefixState: probeGlobalNpmPrefix({ spawn, exists }) }),
290
378
  })
291
379
  }
292
380
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glasspane-install",
3
- "version": "1.25.2",
3
+ "version": "1.26.0",
4
4
  "description": "GlassPane one-command installer: terminal onboarding → install deps → build TS + Swift engine → start daemon → open settings GUI for permission guide",
5
5
  "type": "module",
6
6
  "license": "MIT",