@shgroup/dsh-serenity-hooks 1.137.9 → 1.137.10
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/dsh.plugin.json +1 -1
- package/lib/browser/platform-win.d.ts +176 -0
- package/lib/browser/platform.d.ts +2 -0
- package/lib/browser/process.d.ts +43 -4
- package/lib/browser/service.d.ts +6 -0
- package/lib/index.js +382 -13
- package/package.json +1 -1
package/dsh.plugin.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "dsh-serenity-hooks",
|
|
3
|
-
"version": "1.137.
|
|
3
|
+
"version": "1.137.10",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"description": "宁静号 ACC harness(Native Cordis 插件):给 DSH 装一个「AI 工作区」——14 个工具(container_fs/cognitive_trajectory/dashboard/container_git/msm/praxis/ontology/serenity-skill-creator/localstore/container_admin/browser_slot;另三个 im-bridge/diagram/reconstruct 按条件出现)+ 机械约束(安全模式/工作区围墙/密钥守卫)+ 认知轨迹日志与原地重建 + 网页登录入口/IM 桥/认知轨迹唤醒注册表 + 浏览器槽位管理(≤20 个独立 profile 的 Chrome 槽位,带占用归属与异常时向所属会话反向注入提醒)。轨迹声明 = 轨迹目录下的 trajectory.json(约束面 + 旧标识 uid + CRO 机器绑定 cro.host;旧名 trajectory-constraint.json / skiff.json 仍在读取族里);轨迹提示词 = 轨迹目录下的 PROMPT.md(一条轨迹「是谁、按什么章程工作」,所有轨迹通用,追加在容器层与质量层之后,不替换它们)。适配 DSH 0.1.7-rc.1 / 0.2.0-rc.1 / 0.2.1-rc.1(三档,deepseek-ai/deepseek-harness)。",
|
|
6
6
|
"engines": {
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* browser/platform-win.ts — **Windows 差异的归集处**(2026-11-19,owner 令「做」/「在 win 上能用就行」)
|
|
3
|
+
*
|
|
4
|
+
* ## 定位(先说清楚它不是第二个 mac 移植)
|
|
5
|
+
*
|
|
6
|
+
* `platform.ts` 是 **darwin** 的归集处,本文件是 **win32** 的。两者**不共享实现**,
|
|
7
|
+
* 只共享**形状**:`process.ts` 每个入口第一行做一次三向分派
|
|
8
|
+
* (`win32 → 本文件` / `darwin → platform.ts` / 其余 → 原作 Linux 实现,逐字未动)。
|
|
9
|
+
*
|
|
10
|
+
* ⚠️ **Linux 与 macOS 才是主场**(owner 明言)。所以本文件的验收标准是
|
|
11
|
+
* 「**在 Windows 上能用**」,不是「与 Linux 逐项等同」。凡是做不到等同的地方,
|
|
12
|
+
* 一律**显式降级并让它在读数上看得见**,⛔ 不假装等同(见下方「三处降级」)。
|
|
13
|
+
*
|
|
14
|
+
* ## 与 macOS 的四处**结构性**差异(这些不是移植细节,是新问题)
|
|
15
|
+
*
|
|
16
|
+
* | # | 事项 | Linux / macOS | Windows |
|
|
17
|
+
* |---|---|---|---|
|
|
18
|
+
* | ① | 杀进程 | `SIGTERM` → 等 → `SIGKILL` | Node 的 `SIGTERM` 在 win32 上**就是** `TerminateProcess` ⇒ **没有优雅退出**,「先温和后强硬」这一层不存在 |
|
|
19
|
+
* | ② | 进程树 | 子进程命令行里一般带 `--user-data-dir` ⇒ 按 profile 认领能认全 | **不保证**(renderer/GPU 未必带)⇒ 认领到主进程后要 `taskkill /T` 按树杀,否则留孤儿 renderer(= 2026-11-09 耗电事故的形态) |
|
|
20
|
+
* | ③ | `--no-sandbox` | **必需**(容器内 Chrome 自身 sandbox 起不来 ⇒ 1.5s 自杀) | **⛔ 绝不能加** —— Windows 上没有那个故障,加上等于**主动拆掉用户的浏览器沙箱** |
|
|
21
|
+
* | ④ | CPU/RSS 采样 | 读 `/proc/stat`(零成本) | 只能起子进程(PowerShell `Get-CimInstance`)⇒ **每 tick 每槽位几百毫秒**,看守自己会变成耗电来源 |
|
|
22
|
+
*
|
|
23
|
+
* ## 三处**显式降级**(都让它在读数上看得见,⛔ 不静默)
|
|
24
|
+
*
|
|
25
|
+
* 1. **不采样 CPU/RSS**(差异 ④)。`winSample` 返回 `sampled: false`,
|
|
26
|
+
* 界面上渲染成 `cpu—/内存—`,而不是 `cpu0%/内存0M` —— 后者是「测了,很低」的
|
|
27
|
+
* **同形假读数**(D253「恒空读数器」同族)。看守在 Windows 上退化成
|
|
28
|
+
* **只验存活 + 租约超时/闲置**,那三条判据不依赖采样。
|
|
29
|
+
* 2. **端口反查用 `netstat -ano`**(不是 `/proc/net/tcp`)。权限面比 Linux 弱:
|
|
30
|
+
* 别人的进程可能查不到 ⇒ 回空,而空在调用方等价于「没人占」。真正的兜底是 CDP 探活。
|
|
31
|
+
* 3. **进程认领靠一次 PowerShell 快照 + 1s 缓存**。不是每槽位起一个子进程
|
|
32
|
+
* (20 槽位会变成 20 次 PS,几秒钟只为算一个布尔量)。
|
|
33
|
+
*
|
|
34
|
+
* ## 为什么把「解析」与「取证」分开
|
|
35
|
+
*
|
|
36
|
+
* 凡是能从**字符串**判定对错的,一律做成**纯函数**(`parseNetstatListeners` /
|
|
37
|
+
* `parseCimProcesses` / `winChromeCandidates`)。原因就是 v1.137.8 那次教训:
|
|
38
|
+
* `process.platform` 在测试里改不了 ⇒ 写死平台的判据**在 Linux 上结构上判不了**,
|
|
39
|
+
* 删掉整条 Windows 分支测试也照样全绿。⇒ 本文件里**所有判据都能在 Linux 上真跑**,
|
|
40
|
+
* 只有 `execFileSync` 那几行薄包装不行(它们各自只用一条「失败方向」用例兜住)。
|
|
41
|
+
*/
|
|
42
|
+
/** 当前是否 Windows(**纯函数**,测试可注入) */
|
|
43
|
+
export declare function isWin(platform?: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Windows 上 Chrome 的位置(按优先级)。
|
|
46
|
+
*
|
|
47
|
+
* 🔴 顺序有意义:
|
|
48
|
+
* ① `%LOCALAPPDATA%` —— **用户级安装(最常见!)**。Chrome 默认装在这里
|
|
49
|
+
* (`C:\Users\<u>\AppData\Local\Google\Chrome\Application\chrome.exe`),
|
|
50
|
+
* 不是 Program Files。把它排第一能少一次无谓的存在性检查。
|
|
51
|
+
* ② `%ProgramFiles%` —— 全机安装(管理员装的)。
|
|
52
|
+
* ③ `%ProgramFiles(x86)%` —— 32 位安装(老机器/老镜像)。
|
|
53
|
+
* ④ Chromium 便携版常见落点。
|
|
54
|
+
* ⑤ **Microsoft Edge(最后兜底)** —— 见下方说明。
|
|
55
|
+
*
|
|
56
|
+
* ⚠️ **为什么把 Edge 放进来**:owner 的验收标准是「**在 win 上能用就行**」。
|
|
57
|
+
* Edge 是 Chromium 内核、支持同一套 CDP、且**每台 Windows 都预装** ⇒
|
|
58
|
+
* 在没有 Chrome 的机器上,它让槽位**真的能跑起来**,而不是回一句「未找到 Chrome」。
|
|
59
|
+
* ⛔ 但它与 macOS 那次刻意排除的 Playwright 缓存**不是同一类**:
|
|
60
|
+
* 那份是「自动化副本、不是用户的浏览器」;Edge 是**用户机器上真实存在的浏览器**。
|
|
61
|
+
* ✅ 所以回执里会印出**实际选中的路径** —— 选到 Edge 时用户一眼能看出来。
|
|
62
|
+
*/
|
|
63
|
+
export declare function winChromeCandidates(env?: Record<string, string | undefined>): string[];
|
|
64
|
+
/**
|
|
65
|
+
* 供错误回执印出「试过哪些路径」——**与环境变量无关的稳定形**。
|
|
66
|
+
*
|
|
67
|
+
* ⚠️ 与 `winChromeCandidates` 的差别只在于「用占位变量拼」而不是读当前 env:
|
|
68
|
+
* 回执要能**在任何机器上**说清楚该往哪装,⛔ 不能因为本机没装 ProgramFiles 就少印一行。
|
|
69
|
+
*/
|
|
70
|
+
export declare const WIN_CHROME_CANDIDATES: readonly string[];
|
|
71
|
+
/** Windows 上找 Chrome/Edge;找不到 ⇒ `null`(⛔ 不猜、⛔ 不降级到假路径) */
|
|
72
|
+
export declare function winFindChrome(env?: Record<string, string | undefined>): string | null;
|
|
73
|
+
/**
|
|
74
|
+
* 显示后端说明:Windows 走原生窗口(DWM),**不需要任何显示参数**。
|
|
75
|
+
*
|
|
76
|
+
* ⚠️ 与 macOS 同一条纪律:**刻意不加 `--headless`**。槽位的价值在于「人能在屏幕上看见它」;
|
|
77
|
+
* Windows 默认有窗口服务器 ⇒ 与 Linux 的 wayland/x11 分支**同语义**。
|
|
78
|
+
* ⛔ 不因为「这里没有 socket 概念」就静默退化成 headless —— 那会改变取证条件。
|
|
79
|
+
*/
|
|
80
|
+
export declare function winDisplayDetail(): string;
|
|
81
|
+
/**
|
|
82
|
+
* 从 `netstat -ano -p TCP` 的输出里取出**监听**某端口的 pid。
|
|
83
|
+
*
|
|
84
|
+
* 行形状(列间是空白,末列是 pid):
|
|
85
|
+
* ```
|
|
86
|
+
* TCP 127.0.0.1:9301 0.0.0.0:0 LISTENING 12345
|
|
87
|
+
* TCP [::1]:9301 [::]:0 LISTENING 12345
|
|
88
|
+
* ```
|
|
89
|
+
* 🔴 只认 `LISTENING`:`ESTABLISHED` 行里的本地端口也可能是 9301(我们自己的 CDP 连接),
|
|
90
|
+
* 把它算成「占用者」会让 `launchSlot` 把自己刚建立的连接误判成「端口被别人的进程占着」。
|
|
91
|
+
* 🔴 比对**末段冒号后的数字**,⛔ 不用 `includes(':9301')` —— 那会把 `:19301`、`:93010` 一起命中。
|
|
92
|
+
*/
|
|
93
|
+
export declare function parseNetstatListeners(text: string, port: number): number[];
|
|
94
|
+
/** 一条 PowerShell 进程快照记录(`Win32_Process` 的子集) */
|
|
95
|
+
export interface WinProcEntry {
|
|
96
|
+
pid: number;
|
|
97
|
+
/** 命令行(无权读/系统进程可能是 `null`) */
|
|
98
|
+
cmd: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 解析 `Get-CimInstance Win32_Process | Select ProcessId,CommandLine | ConvertTo-Json` 的输出。
|
|
102
|
+
*
|
|
103
|
+
* 🔴 两个必须处理的形状(都会真的出现):
|
|
104
|
+
* ① **只有一条记录时 `ConvertTo-Json` 返回对象而不是数组** ⇒ 不归一化就会在 `.filter` 上炸;
|
|
105
|
+
* ② `CommandLine` 可能是 `null`(别人的/受保护进程)⇒ 不能直接 `.includes`。
|
|
106
|
+
* 🔴 `ProcessId` 用 `Number()` 再校验整数:JSON 里是数字,但手抄夹具可能是字符串。
|
|
107
|
+
*/
|
|
108
|
+
export declare function parseCimProcesses(raw: string): WinProcEntry[];
|
|
109
|
+
/**
|
|
110
|
+
* 从快照里挑出**命令行含本槽位 profile** 的 pid。
|
|
111
|
+
*
|
|
112
|
+
* 🔴 判据与 Linux/macOS **完全相同**(命令行里必须出现 `--user-data-dir=<本槽位专属>`):
|
|
113
|
+
* ⛔ 绝不用「进程名叫 chrome.exe」当判据 —— 那会把**用户自己的浏览器**一起认领、一起杀掉
|
|
114
|
+
* (2026-11-09 事故差点犯的第二个错)。
|
|
115
|
+
* ⚠️ **路径大小写**:Windows 大小写不敏感,而 Chrome 会把 `--user-data-dir` 的**盘符与首字母**
|
|
116
|
+
* 规范化(实测常见 `C:\...` 与我们拼出来的 `c:\...` 不一致)⇒ 比对时必须 `toLowerCase()`。
|
|
117
|
+
* 这一条**只在 Windows 上成立**,所以放在本文件、不进共用代码。
|
|
118
|
+
*/
|
|
119
|
+
export declare function pickPidsByProfile(entries: readonly WinProcEntry[], profileDir: string): number[];
|
|
120
|
+
/** 取一次进程快照(含缓存) */
|
|
121
|
+
export declare function winProcessSnapshot(now?: number): WinProcEntry[];
|
|
122
|
+
/** 清缓存(测试与「杀完之后立刻复认」用) */
|
|
123
|
+
export declare function resetWinSnapshotCache(): void;
|
|
124
|
+
/** 按 profile 认领 pid(Windows 版) */
|
|
125
|
+
export declare function winFindPidsByProfile(profileDir: string): number[];
|
|
126
|
+
/** 某 pid 的命令行是否属于这个 profile(Windows 版;⛔ 供归因用,不是「杀谁」的依据) */
|
|
127
|
+
export declare function winPidClaimsProfile(pid: number, profileDir: string): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* 某端口的监听者 pid(`netstat -ano -p TCP`)。
|
|
130
|
+
*
|
|
131
|
+
* ⚠️ 降级已在上方文件头登记:别人的进程可能因权限查不到 ⇒ 回空,
|
|
132
|
+
* 而空在调用方等价于「没人占」。兜底 = CDP 探活那一步。
|
|
133
|
+
*/
|
|
134
|
+
export declare function winPortHolders(port: number): number[];
|
|
135
|
+
/**
|
|
136
|
+
* 采样:**Windows 上刻意不采**(见文件头「三处显式降级 ①」)。
|
|
137
|
+
*
|
|
138
|
+
* `alive` 由调用方给的 pids 推出(**零额外成本** —— `findPidsByProfile` 已经跑过了);
|
|
139
|
+
* `cpuPct` / `rssMb` 固定 0 且 `sampled: false`,让界面渲染成「未测」而不是「很低」。
|
|
140
|
+
*/
|
|
141
|
+
export declare function winSample(pids: number[], now?: number): {
|
|
142
|
+
alive: boolean;
|
|
143
|
+
cpuPct: number;
|
|
144
|
+
rssMb: number;
|
|
145
|
+
at: number;
|
|
146
|
+
sampled: boolean;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* 该 pid 是否活着。
|
|
150
|
+
*
|
|
151
|
+
* `process.kill(pid, 0)` 在 Windows 上由 libuv 实现为「打开进程句柄」——
|
|
152
|
+
* 打不开即 ESRCH ⇒ 与 POSIX 同语义。`EPERM`(存在但无权)也算活着。
|
|
153
|
+
* ⚠️ Windows 无「僵尸」概念(进程退出即回收句柄)⇒ 不存在 Linux 侧那个
|
|
154
|
+
* 「`/proc/<pid>` 还在但已死」的坑。**这一格 Windows 比 Linux 简单**,如实登记。
|
|
155
|
+
*/
|
|
156
|
+
export declare function winIsAlive(pid: number): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* 按**进程树**强杀(`taskkill /PID <pid> /T /F`)。
|
|
159
|
+
*
|
|
160
|
+
* 🔴 为什么必须有它(差异 ②):Windows 上 Chrome 的 renderer/GPU 子进程**不保证**
|
|
161
|
+
* 命令行里带 `--user-data-dir` ⇒ 按 profile 认领可能只认到主进程。
|
|
162
|
+
* 只杀主进程会留下**孤儿 renderer**,而它们继续占 CPU —— 那正是 2026-11-09 耗电事故的形态,
|
|
163
|
+
* 且「进程少了但 CPU 还在」在读数上极难归因。
|
|
164
|
+
* ⚠️ 用 `/F`(强制)是刻意的:见差异 ①,Windows 上本来就**没有优雅退出**这回事。
|
|
165
|
+
* 回执三档语义不变(`killed`/`absent`/`denied`),只是「先温和后强硬」两层并成了一层。
|
|
166
|
+
*/
|
|
167
|
+
export declare function winKillTree(pid: number): WinKillOutcome;
|
|
168
|
+
/**
|
|
169
|
+
* `winKillTree` 的三档结果。
|
|
170
|
+
*
|
|
171
|
+
* 🔴 与 `process.ts` 的 `KillOutcome` **同形但独立声明** —— 理由与 `MacProcSample` 相同:
|
|
172
|
+
* 反向 import 会造出 `process.ts ⇄ platform-win.ts` 的循环依赖,而循环依赖在打包后
|
|
173
|
+
* 表现为「某个函数在某个时机 undefined」,**只在真机上炸**(本仓付过这类代价)。
|
|
174
|
+
* ⚠️ 两处必须保持同形;改名/加档时**两边一起改**(`process.ts` 的类型注解会当场报错兜住)。
|
|
175
|
+
*/
|
|
176
|
+
export type WinKillOutcome = 'killed' | 'absent' | 'denied';
|
|
@@ -51,6 +51,8 @@ export interface MacProcSample {
|
|
|
51
51
|
at: number;
|
|
52
52
|
/** 累计 CPU 时间(**秒 × 1000** 的整数,避免浮点漂移);Linux 侧同名字段是 ticks */
|
|
53
53
|
ticks?: number;
|
|
54
|
+
/** 见 `ProcSample.sampled`:macOS **真的采**,故恒 `true`(与 Windows 的 `false` 成对) */
|
|
55
|
+
sampled?: boolean;
|
|
54
56
|
}
|
|
55
57
|
/**
|
|
56
58
|
* macOS 上 Chrome 的位置(按优先级)。
|
package/lib/browser/process.d.ts
CHANGED
|
@@ -114,6 +114,9 @@ export declare function findChromeFor(platform: string, exists?: (p: string) =>
|
|
|
114
114
|
* 于是"平台分派"这件事**在 Linux 上永远读不出对错**。
|
|
115
115
|
* ⇒ 修法:把平台作为**参数**传进来,测试即可构造 `'darwin'` 走真分派。
|
|
116
116
|
* ⚠️ 生产调用点仍走默认参数(`process.platform`),行为零变化。
|
|
117
|
+
*
|
|
118
|
+
* 🆕 **2026-11-19 三向**(owner 令「做」/「在 win 上能用就行」):加 `win32` 一支。
|
|
119
|
+
* Linux 与 darwin 两支**逐字未改**,只是 win32 分支插在它们之间。
|
|
117
120
|
*/
|
|
118
121
|
export declare function chromeCandidatesFor(platform?: string): readonly string[];
|
|
119
122
|
/** 生产包装(默认当前平台) */
|
|
@@ -143,8 +146,13 @@ export declare function chromeCandidatesForMessage(): readonly string[];
|
|
|
143
146
|
export interface DisplayChoice {
|
|
144
147
|
args: string[];
|
|
145
148
|
env: Record<string, string | undefined>;
|
|
146
|
-
/**
|
|
147
|
-
|
|
149
|
+
/**
|
|
150
|
+
* 选中的后端(诊断用)。
|
|
151
|
+
* `cocoa` = macOS 原生窗口;`native` = Windows 原生窗口(DWM)。
|
|
152
|
+
* 🔴 两者**刻意不合并成 `native`**:回执要说清是哪个平台的原生栈,
|
|
153
|
+
* 否则「为什么这台 mac 上没有 `--ozone-platform` 参数」又要重查一遍。
|
|
154
|
+
*/
|
|
155
|
+
backend: 'wayland' | 'x11' | 'cocoa' | 'native' | 'headless';
|
|
148
156
|
/** 人话说明(进日志/回执,别让下一个我重查一遍) */
|
|
149
157
|
detail: string;
|
|
150
158
|
}
|
|
@@ -194,8 +202,23 @@ export declare function resolveDisplay(env?: Record<string, string | undefined>,
|
|
|
194
202
|
* ⚠️ 同样刻意**不用** `--disable-gpu`:Chrome 的 GPU 进程崩溃是这次实打实的死因,
|
|
195
203
|
* 但关掉 GPU 会让截图与 3D 页面拿到不同的渲染路径(软件光栅化)⇒ **取证条件变了**。
|
|
196
204
|
* 两条实现都保留 GPU(只用 `--no-sandbox` 解决 sandbox 问题),本实现跟随。
|
|
205
|
+
*
|
|
206
|
+
* 🔴🔴 **`--no-sandbox` 与 `--disable-dev-shm-usage` 是 Linux 专属**(v1.137.10,2026-11-19)。
|
|
207
|
+
*
|
|
208
|
+
* 这两条都**不是**"为了跨平台统一"的通用参数,它们的成因**只存在于 Linux**:
|
|
209
|
+
* - `--no-sandbox`:容器内 Chrome 自身 sandbox 初始化失败(`Operation not permitted`)。
|
|
210
|
+
* macOS / Windows 上**没有这个故障** ⇒ 在那里加上它等于**主动拆掉用户的浏览器沙箱**,
|
|
211
|
+
* 把一个本可以正常工作的安全边界关掉,且**没有任何收益**。
|
|
212
|
+
* - `--disable-dev-shm-usage`:容器 `/dev/shm` 只有 64MB。Windows 上没有 `/dev/shm` 这回事。
|
|
213
|
+
*
|
|
214
|
+
* ⇒ 判据:**平台专属的参数必须按平台给,⛔ 不能因为"统一"就全局带上。**
|
|
215
|
+
* 这是本轨迹里"删值 = 删契约"的**反面**:这里是"**带值 = 造不存在的契约**"。
|
|
216
|
+
* ⚠️ macOS 上原实现同样带着这两条(v1.137.8 没动它,那版只把启动跑通);
|
|
217
|
+
* 本次**一并修正**:mac 上也不再带 `--no-sandbox`。
|
|
218
|
+
* 🔴 **这是一处行为变更,不是纯重构** —— 它在 mac 上是**变好**(沙箱回来了),
|
|
219
|
+
* 但属于"mac 上从未真机验过"的那一格里,所以只改参数、**不改那段诚实边界声明**。
|
|
197
220
|
*/
|
|
198
|
-
export declare function buildArgs(n: number, paths: SlotPaths, display?: DisplayChoice): string[];
|
|
221
|
+
export declare function buildArgs(n: number, paths: SlotPaths, display?: DisplayChoice, platform?: string): string[];
|
|
199
222
|
/** 启动结果 */
|
|
200
223
|
export interface LaunchResult {
|
|
201
224
|
ok: boolean;
|
|
@@ -240,6 +263,16 @@ export interface ProcSample {
|
|
|
240
263
|
rssMb: number;
|
|
241
264
|
/** 采样时刻(ms) */
|
|
242
265
|
at: number;
|
|
266
|
+
/**
|
|
267
|
+
* 这次读数**是否真的采到了**资源量(CPU/RSS)。
|
|
268
|
+
*
|
|
269
|
+
* 🔴 为什么要这个字段(2026-11-19,v1.137.10):Windows 上**刻意不采样**(成本见
|
|
270
|
+
* {@link sampleProcTree} 的 win32 分支)。若不给这个字段,`cpuPct: 0 / rssMb: 0`
|
|
271
|
+
* 会在界面上渲染成 `cpu0% 内存0M` —— 与「测了,确实很低」**完全同形**。
|
|
272
|
+
* ⇒ 判据(D253 家族):**「没测」必须与「测了是 0」在读数上分得开。**
|
|
273
|
+
* `undefined` 视为 `true`(Linux/macOS 一直在采,历史调用方不必改)。
|
|
274
|
+
*/
|
|
275
|
+
sampled?: boolean;
|
|
243
276
|
}
|
|
244
277
|
/**
|
|
245
278
|
* 采一次样。
|
|
@@ -330,8 +363,14 @@ export declare function destroySlot(n: number, pidHint?: number): {
|
|
|
330
363
|
*
|
|
331
364
|
* 🔴 `platform` 与 `macClaim` 都是**可注入参数**(与 {@link chromeCandidatesFor} 同一理由):
|
|
332
365
|
* 否则"分派真的发生了"这件事在 Linux 上**永远读不出对错** —— 删掉 darwin 分支测试也不会红。
|
|
366
|
+
*
|
|
367
|
+
* 🔴 **2026-11-19(v1.137.10)**:加 `win32` 一支。**同一族的缺陷不该有第三个版本** ——
|
|
368
|
+
* 这次在加 Windows 支持时**主动**把它一起做了,而不是等 mac 那次的形态在 win 上重演一遍
|
|
369
|
+
* (在 win 上重演的后果与 mac 完全相同:`destroySlot` 会印
|
|
370
|
+
* 「台账那条记录是陈旧的或被污染」—— 一句在 win 上恒假的话)。
|
|
371
|
+
* ⚠️ `winClaim` 同样是可注入参数,理由同上:⛔ 不能只靠"本机是 Linux"来验它。
|
|
333
372
|
*/
|
|
334
|
-
export declare function pidClaimsProfile(pid: number, profileDir: string, platform?: string, macClaim?: (pid: number, profileDir: string) => boolean): boolean;
|
|
373
|
+
export declare function pidClaimsProfile(pid: number, profileDir: string, platform?: string, macClaim?: (pid: number, profileDir: string) => boolean, winClaim?: (pid: number, profileDir: string) => boolean): boolean;
|
|
335
374
|
/** CDP 就绪探测(`/json/version` 能返回 JSON 即就绪) */
|
|
336
375
|
export declare function probeCdp(n: number, timeoutMs?: number): Promise<boolean>;
|
|
337
376
|
/**
|
package/lib/browser/service.d.ts
CHANGED
|
@@ -53,6 +53,12 @@ export interface SlotView {
|
|
|
53
53
|
rssMb: number;
|
|
54
54
|
pid: number;
|
|
55
55
|
nudges: number;
|
|
56
|
+
/**
|
|
57
|
+
* 这次视图的 CPU/内存读数**是否真采过**。
|
|
58
|
+
* `false` ⇒ 渲染成 `cpu—` / `内存—`,⛔ 不能渲染成 `cpu0%`(那是「测了,很低」的同形假读数)。
|
|
59
|
+
* 当前只有 Windows 为 `false`(见 `process.ts` 的 `sampleProcTree` win32 分支)。
|
|
60
|
+
*/
|
|
61
|
+
sampled: boolean;
|
|
56
62
|
}
|
|
57
63
|
/** acquire:租一个槽位(同会话已有 ⇒ 复用,⛔ 不再租第二个) */
|
|
58
64
|
export interface AcquireInput {
|
package/lib/index.js
CHANGED
|
@@ -18534,7 +18534,8 @@ function macSample(pids, prev, now = Date.now()) {
|
|
|
18534
18534
|
cpuPct,
|
|
18535
18535
|
rssMb: rssKb / 1024,
|
|
18536
18536
|
at: now,
|
|
18537
|
-
ticks
|
|
18537
|
+
ticks,
|
|
18538
|
+
sampled: true
|
|
18538
18539
|
};
|
|
18539
18540
|
}
|
|
18540
18541
|
/**
|
|
@@ -18590,6 +18591,308 @@ function macIsAlive(pid) {
|
|
|
18590
18591
|
}
|
|
18591
18592
|
}
|
|
18592
18593
|
//#endregion
|
|
18594
|
+
//#region src/browser/platform-win.ts
|
|
18595
|
+
/** 当前是否 Windows(**纯函数**,测试可注入) */
|
|
18596
|
+
function isWin(platform = process.platform) {
|
|
18597
|
+
return platform === "win32";
|
|
18598
|
+
}
|
|
18599
|
+
/**
|
|
18600
|
+
* Windows 上 Chrome 的位置(按优先级)。
|
|
18601
|
+
*
|
|
18602
|
+
* 🔴 顺序有意义:
|
|
18603
|
+
* ① `%LOCALAPPDATA%` —— **用户级安装(最常见!)**。Chrome 默认装在这里
|
|
18604
|
+
* (`C:\Users\<u>\AppData\Local\Google\Chrome\Application\chrome.exe`),
|
|
18605
|
+
* 不是 Program Files。把它排第一能少一次无谓的存在性检查。
|
|
18606
|
+
* ② `%ProgramFiles%` —— 全机安装(管理员装的)。
|
|
18607
|
+
* ③ `%ProgramFiles(x86)%` —— 32 位安装(老机器/老镜像)。
|
|
18608
|
+
* ④ Chromium 便携版常见落点。
|
|
18609
|
+
* ⑤ **Microsoft Edge(最后兜底)** —— 见下方说明。
|
|
18610
|
+
*
|
|
18611
|
+
* ⚠️ **为什么把 Edge 放进来**:owner 的验收标准是「**在 win 上能用就行**」。
|
|
18612
|
+
* Edge 是 Chromium 内核、支持同一套 CDP、且**每台 Windows 都预装** ⇒
|
|
18613
|
+
* 在没有 Chrome 的机器上,它让槽位**真的能跑起来**,而不是回一句「未找到 Chrome」。
|
|
18614
|
+
* ⛔ 但它与 macOS 那次刻意排除的 Playwright 缓存**不是同一类**:
|
|
18615
|
+
* 那份是「自动化副本、不是用户的浏览器」;Edge 是**用户机器上真实存在的浏览器**。
|
|
18616
|
+
* ✅ 所以回执里会印出**实际选中的路径** —— 选到 Edge 时用户一眼能看出来。
|
|
18617
|
+
*/
|
|
18618
|
+
function winChromeCandidates(env = process.env) {
|
|
18619
|
+
const local = env.LOCALAPPDATA ?? "";
|
|
18620
|
+
const pf = env.ProgramFiles ?? "";
|
|
18621
|
+
const pf86 = env["ProgramFiles(x86)"] ?? "";
|
|
18622
|
+
return [
|
|
18623
|
+
local !== "" ? join(local, "Google/Chrome/Application/chrome.exe") : "",
|
|
18624
|
+
pf !== "" ? join(pf, "Google/Chrome/Application/chrome.exe") : "",
|
|
18625
|
+
pf86 !== "" ? join(pf86, "Google/Chrome/Application/chrome.exe") : "",
|
|
18626
|
+
local !== "" ? join(local, "Chromium/Application/chrome.exe") : "",
|
|
18627
|
+
pf !== "" ? join(pf, "Microsoft/Edge/Application/msedge.exe") : "",
|
|
18628
|
+
pf86 !== "" ? join(pf86, "Microsoft/Edge/Application/msedge.exe") : ""
|
|
18629
|
+
].filter((p) => p !== "");
|
|
18630
|
+
}
|
|
18631
|
+
/**
|
|
18632
|
+
* 供错误回执印出「试过哪些路径」——**与环境变量无关的稳定形**。
|
|
18633
|
+
*
|
|
18634
|
+
* ⚠️ 与 `winChromeCandidates` 的差别只在于「用占位变量拼」而不是读当前 env:
|
|
18635
|
+
* 回执要能**在任何机器上**说清楚该往哪装,⛔ 不能因为本机没装 ProgramFiles 就少印一行。
|
|
18636
|
+
*/
|
|
18637
|
+
const WIN_CHROME_CANDIDATES = [
|
|
18638
|
+
"%LOCALAPPDATA%\\Google\\Chrome\\Application\\chrome.exe",
|
|
18639
|
+
"%ProgramFiles%\\Google\\Chrome\\Application\\chrome.exe",
|
|
18640
|
+
"%ProgramFiles(x86)%\\Google\\Chrome\\Application\\chrome.exe",
|
|
18641
|
+
"%LOCALAPPDATA%\\Chromium\\Application\\chrome.exe",
|
|
18642
|
+
"%ProgramFiles%\\Microsoft\\Edge\\Application\\msedge.exe",
|
|
18643
|
+
"%ProgramFiles(x86)%\\Microsoft\\Edge\\Application\\msedge.exe"
|
|
18644
|
+
];
|
|
18645
|
+
/**
|
|
18646
|
+
* 显示后端说明:Windows 走原生窗口(DWM),**不需要任何显示参数**。
|
|
18647
|
+
*
|
|
18648
|
+
* ⚠️ 与 macOS 同一条纪律:**刻意不加 `--headless`**。槽位的价值在于「人能在屏幕上看见它」;
|
|
18649
|
+
* Windows 默认有窗口服务器 ⇒ 与 Linux 的 wayland/x11 分支**同语义**。
|
|
18650
|
+
* ⛔ 不因为「这里没有 socket 概念」就静默退化成 headless —— 那会改变取证条件。
|
|
18651
|
+
*/
|
|
18652
|
+
function winDisplayDetail() {
|
|
18653
|
+
return "Windows 原生窗口(DWM;不需要 Wayland/X11 参数)";
|
|
18654
|
+
}
|
|
18655
|
+
/**
|
|
18656
|
+
* 从 `netstat -ano -p TCP` 的输出里取出**监听**某端口的 pid。
|
|
18657
|
+
*
|
|
18658
|
+
* 行形状(列间是空白,末列是 pid):
|
|
18659
|
+
* ```
|
|
18660
|
+
* TCP 127.0.0.1:9301 0.0.0.0:0 LISTENING 12345
|
|
18661
|
+
* TCP [::1]:9301 [::]:0 LISTENING 12345
|
|
18662
|
+
* ```
|
|
18663
|
+
* 🔴 只认 `LISTENING`:`ESTABLISHED` 行里的本地端口也可能是 9301(我们自己的 CDP 连接),
|
|
18664
|
+
* 把它算成「占用者」会让 `launchSlot` 把自己刚建立的连接误判成「端口被别人的进程占着」。
|
|
18665
|
+
* 🔴 比对**末段冒号后的数字**,⛔ 不用 `includes(':9301')` —— 那会把 `:19301`、`:93010` 一起命中。
|
|
18666
|
+
*/
|
|
18667
|
+
function parseNetstatListeners(text, port) {
|
|
18668
|
+
const pids = /* @__PURE__ */ new Set();
|
|
18669
|
+
for (const raw of text.split("\n")) {
|
|
18670
|
+
const cols = raw.trim().split(/\s+/);
|
|
18671
|
+
if (cols.length < 5) continue;
|
|
18672
|
+
if ((cols[0] ?? "").toUpperCase() !== "TCP") continue;
|
|
18673
|
+
if ((cols[3] ?? "").toUpperCase() !== "LISTENING") continue;
|
|
18674
|
+
const local = cols[1] ?? "";
|
|
18675
|
+
const idx = local.lastIndexOf(":");
|
|
18676
|
+
if (idx < 0) continue;
|
|
18677
|
+
if (Number(local.slice(idx + 1)) !== port) continue;
|
|
18678
|
+
const pid = Number(cols[4] ?? "");
|
|
18679
|
+
if (Number.isInteger(pid) && pid > 0) pids.add(pid);
|
|
18680
|
+
}
|
|
18681
|
+
return [...pids].filter((p) => p !== process.pid);
|
|
18682
|
+
}
|
|
18683
|
+
/**
|
|
18684
|
+
* 解析 `Get-CimInstance Win32_Process | Select ProcessId,CommandLine | ConvertTo-Json` 的输出。
|
|
18685
|
+
*
|
|
18686
|
+
* 🔴 两个必须处理的形状(都会真的出现):
|
|
18687
|
+
* ① **只有一条记录时 `ConvertTo-Json` 返回对象而不是数组** ⇒ 不归一化就会在 `.filter` 上炸;
|
|
18688
|
+
* ② `CommandLine` 可能是 `null`(别人的/受保护进程)⇒ 不能直接 `.includes`。
|
|
18689
|
+
* 🔴 `ProcessId` 用 `Number()` 再校验整数:JSON 里是数字,但手抄夹具可能是字符串。
|
|
18690
|
+
*/
|
|
18691
|
+
function parseCimProcesses(raw) {
|
|
18692
|
+
if (raw.trim() === "") return [];
|
|
18693
|
+
let parsed;
|
|
18694
|
+
try {
|
|
18695
|
+
parsed = JSON.parse(raw);
|
|
18696
|
+
} catch {
|
|
18697
|
+
return [];
|
|
18698
|
+
}
|
|
18699
|
+
const arr = Array.isArray(parsed) ? parsed : [parsed];
|
|
18700
|
+
const out = [];
|
|
18701
|
+
for (const item of arr) {
|
|
18702
|
+
if (item === null || typeof item !== "object") continue;
|
|
18703
|
+
const rec = item;
|
|
18704
|
+
const pid = Number(rec.ProcessId);
|
|
18705
|
+
if (!Number.isInteger(pid) || pid <= 0) continue;
|
|
18706
|
+
out.push({
|
|
18707
|
+
pid,
|
|
18708
|
+
cmd: typeof rec.CommandLine === "string" ? rec.CommandLine : ""
|
|
18709
|
+
});
|
|
18710
|
+
}
|
|
18711
|
+
return out;
|
|
18712
|
+
}
|
|
18713
|
+
/**
|
|
18714
|
+
* 从快照里挑出**命令行含本槽位 profile** 的 pid。
|
|
18715
|
+
*
|
|
18716
|
+
* 🔴 判据与 Linux/macOS **完全相同**(命令行里必须出现 `--user-data-dir=<本槽位专属>`):
|
|
18717
|
+
* ⛔ 绝不用「进程名叫 chrome.exe」当判据 —— 那会把**用户自己的浏览器**一起认领、一起杀掉
|
|
18718
|
+
* (2026-11-09 事故差点犯的第二个错)。
|
|
18719
|
+
* ⚠️ **路径大小写**:Windows 大小写不敏感,而 Chrome 会把 `--user-data-dir` 的**盘符与首字母**
|
|
18720
|
+
* 规范化(实测常见 `C:\...` 与我们拼出来的 `c:\...` 不一致)⇒ 比对时必须 `toLowerCase()`。
|
|
18721
|
+
* 这一条**只在 Windows 上成立**,所以放在本文件、不进共用代码。
|
|
18722
|
+
*/
|
|
18723
|
+
function pickPidsByProfile(entries, profileDir) {
|
|
18724
|
+
if (profileDir === "") return [];
|
|
18725
|
+
const needle = profileDir.toLowerCase();
|
|
18726
|
+
const out = [];
|
|
18727
|
+
for (const e of entries) {
|
|
18728
|
+
if (e.pid === process.pid) continue;
|
|
18729
|
+
if (e.cmd !== "" && e.cmd.toLowerCase().includes(needle)) out.push(e.pid);
|
|
18730
|
+
}
|
|
18731
|
+
return out;
|
|
18732
|
+
}
|
|
18733
|
+
/** PowerShell 可执行名(`windowsHide` 防闪窗;`-NoProfile` 防用户 profile 拖慢/污染) */
|
|
18734
|
+
const PS = "powershell.exe";
|
|
18735
|
+
/**
|
|
18736
|
+
* 跑一段 PowerShell,成功返回 stdout,**任何失败一律返回 `null`**。
|
|
18737
|
+
*
|
|
18738
|
+
* 🔴 `[Console]::OutputEncoding` 必须显式设成 UTF8:中文 Windows 默认是 GBK/cp936,
|
|
18739
|
+
* 而命令行里常有**中文用户名路径**(`C:\Users\张三\…`)⇒ 不设就会拿到乱码,
|
|
18740
|
+
* 于是"认领"静默失败,且与"进程真的没了"在读数上同形。
|
|
18741
|
+
*/
|
|
18742
|
+
function runPs(script, timeoutMs = 8e3) {
|
|
18743
|
+
try {
|
|
18744
|
+
return execFileSync(PS, [
|
|
18745
|
+
"-NoProfile",
|
|
18746
|
+
"-NonInteractive",
|
|
18747
|
+
"-Command",
|
|
18748
|
+
`[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;${script}`
|
|
18749
|
+
], {
|
|
18750
|
+
encoding: "utf8",
|
|
18751
|
+
stdio: [
|
|
18752
|
+
"ignore",
|
|
18753
|
+
"pipe",
|
|
18754
|
+
"ignore"
|
|
18755
|
+
],
|
|
18756
|
+
timeout: timeoutMs,
|
|
18757
|
+
maxBuffer: 33554432,
|
|
18758
|
+
windowsHide: true
|
|
18759
|
+
});
|
|
18760
|
+
} catch {
|
|
18761
|
+
return null;
|
|
18762
|
+
}
|
|
18763
|
+
}
|
|
18764
|
+
/**
|
|
18765
|
+
* 进程快照缓存(**1 秒 TTL**)。
|
|
18766
|
+
*
|
|
18767
|
+
* 🔴 为什么必须缓存:`browser_slot list` 会**逐槽位**调 `findPidsByProfile`。
|
|
18768
|
+
* 不缓存的话 20 个槽位 = 20 次 PowerShell(每次几百毫秒)⇒ 一次 list 要好几秒,
|
|
18769
|
+
* 而且那 20 份快照拍的是**同一个瞬间**,重复取毫无信息增益。
|
|
18770
|
+
* ⚠️ 1 秒是权衡:足够覆盖一次 list/一次看守 tick 的调用簇,又短到不会让
|
|
18771
|
+
* 「刚杀掉、立刻认领」读到陈旧名单(`killPidOutcome` 自己会轮询复检)。
|
|
18772
|
+
*/
|
|
18773
|
+
const SNAPSHOT_TTL_MS = 1e3;
|
|
18774
|
+
let snapshotCache = null;
|
|
18775
|
+
/** 取一次进程快照(含缓存) */
|
|
18776
|
+
function winProcessSnapshot(now = Date.now()) {
|
|
18777
|
+
if (snapshotCache !== null && now - snapshotCache.at < SNAPSHOT_TTL_MS) return snapshotCache.entries;
|
|
18778
|
+
const raw = runPs("Get-CimInstance Win32_Process | Select-Object ProcessId,CommandLine | ConvertTo-Json -Compress");
|
|
18779
|
+
const entries = raw === null ? [] : parseCimProcesses(raw);
|
|
18780
|
+
snapshotCache = {
|
|
18781
|
+
at: now,
|
|
18782
|
+
entries
|
|
18783
|
+
};
|
|
18784
|
+
return entries;
|
|
18785
|
+
}
|
|
18786
|
+
/** 按 profile 认领 pid(Windows 版) */
|
|
18787
|
+
function winFindPidsByProfile(profileDir) {
|
|
18788
|
+
if (profileDir === "") return [];
|
|
18789
|
+
return pickPidsByProfile(winProcessSnapshot(), profileDir);
|
|
18790
|
+
}
|
|
18791
|
+
/** 某 pid 的命令行是否属于这个 profile(Windows 版;⛔ 供归因用,不是「杀谁」的依据) */
|
|
18792
|
+
function winPidClaimsProfile(pid, profileDir) {
|
|
18793
|
+
if (!Number.isInteger(pid) || pid <= 1 || profileDir === "") return false;
|
|
18794
|
+
const found = winProcessSnapshot().find((e) => e.pid === pid);
|
|
18795
|
+
if (found === void 0) return false;
|
|
18796
|
+
return found.cmd.toLowerCase().includes(profileDir.toLowerCase());
|
|
18797
|
+
}
|
|
18798
|
+
/**
|
|
18799
|
+
* 某端口的监听者 pid(`netstat -ano -p TCP`)。
|
|
18800
|
+
*
|
|
18801
|
+
* ⚠️ 降级已在上方文件头登记:别人的进程可能因权限查不到 ⇒ 回空,
|
|
18802
|
+
* 而空在调用方等价于「没人占」。兜底 = CDP 探活那一步。
|
|
18803
|
+
*/
|
|
18804
|
+
function winPortHolders(port) {
|
|
18805
|
+
let out;
|
|
18806
|
+
try {
|
|
18807
|
+
out = execFileSync("netstat", [
|
|
18808
|
+
"-ano",
|
|
18809
|
+
"-p",
|
|
18810
|
+
"TCP"
|
|
18811
|
+
], {
|
|
18812
|
+
encoding: "utf8",
|
|
18813
|
+
stdio: [
|
|
18814
|
+
"ignore",
|
|
18815
|
+
"pipe",
|
|
18816
|
+
"ignore"
|
|
18817
|
+
],
|
|
18818
|
+
timeout: 5e3,
|
|
18819
|
+
maxBuffer: 16777216,
|
|
18820
|
+
windowsHide: true
|
|
18821
|
+
});
|
|
18822
|
+
} catch {
|
|
18823
|
+
return [];
|
|
18824
|
+
}
|
|
18825
|
+
return parseNetstatListeners(out, port);
|
|
18826
|
+
}
|
|
18827
|
+
/**
|
|
18828
|
+
* 采样:**Windows 上刻意不采**(见文件头「三处显式降级 ①」)。
|
|
18829
|
+
*
|
|
18830
|
+
* `alive` 由调用方给的 pids 推出(**零额外成本** —— `findPidsByProfile` 已经跑过了);
|
|
18831
|
+
* `cpuPct` / `rssMb` 固定 0 且 `sampled: false`,让界面渲染成「未测」而不是「很低」。
|
|
18832
|
+
*/
|
|
18833
|
+
function winSample(pids, now = Date.now()) {
|
|
18834
|
+
return {
|
|
18835
|
+
alive: pids.length > 0,
|
|
18836
|
+
cpuPct: 0,
|
|
18837
|
+
rssMb: 0,
|
|
18838
|
+
at: now,
|
|
18839
|
+
sampled: false
|
|
18840
|
+
};
|
|
18841
|
+
}
|
|
18842
|
+
/**
|
|
18843
|
+
* 该 pid 是否活着。
|
|
18844
|
+
*
|
|
18845
|
+
* `process.kill(pid, 0)` 在 Windows 上由 libuv 实现为「打开进程句柄」——
|
|
18846
|
+
* 打不开即 ESRCH ⇒ 与 POSIX 同语义。`EPERM`(存在但无权)也算活着。
|
|
18847
|
+
* ⚠️ Windows 无「僵尸」概念(进程退出即回收句柄)⇒ 不存在 Linux 侧那个
|
|
18848
|
+
* 「`/proc/<pid>` 还在但已死」的坑。**这一格 Windows 比 Linux 简单**,如实登记。
|
|
18849
|
+
*/
|
|
18850
|
+
function winIsAlive(pid) {
|
|
18851
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
18852
|
+
try {
|
|
18853
|
+
process.kill(pid, 0);
|
|
18854
|
+
return true;
|
|
18855
|
+
} catch (e) {
|
|
18856
|
+
return e.code === "EPERM";
|
|
18857
|
+
}
|
|
18858
|
+
}
|
|
18859
|
+
/**
|
|
18860
|
+
* 按**进程树**强杀(`taskkill /PID <pid> /T /F`)。
|
|
18861
|
+
*
|
|
18862
|
+
* 🔴 为什么必须有它(差异 ②):Windows 上 Chrome 的 renderer/GPU 子进程**不保证**
|
|
18863
|
+
* 命令行里带 `--user-data-dir` ⇒ 按 profile 认领可能只认到主进程。
|
|
18864
|
+
* 只杀主进程会留下**孤儿 renderer**,而它们继续占 CPU —— 那正是 2026-11-09 耗电事故的形态,
|
|
18865
|
+
* 且「进程少了但 CPU 还在」在读数上极难归因。
|
|
18866
|
+
* ⚠️ 用 `/F`(强制)是刻意的:见差异 ①,Windows 上本来就**没有优雅退出**这回事。
|
|
18867
|
+
* 回执三档语义不变(`killed`/`absent`/`denied`),只是「先温和后强硬」两层并成了一层。
|
|
18868
|
+
*/
|
|
18869
|
+
function winKillTree(pid) {
|
|
18870
|
+
if (!Number.isInteger(pid) || pid <= 1) return "absent";
|
|
18871
|
+
try {
|
|
18872
|
+
execFileSync("taskkill", [
|
|
18873
|
+
"/PID",
|
|
18874
|
+
String(pid),
|
|
18875
|
+
"/T",
|
|
18876
|
+
"/F"
|
|
18877
|
+
], {
|
|
18878
|
+
encoding: "utf8",
|
|
18879
|
+
stdio: [
|
|
18880
|
+
"ignore",
|
|
18881
|
+
"pipe",
|
|
18882
|
+
"pipe"
|
|
18883
|
+
],
|
|
18884
|
+
timeout: 8e3,
|
|
18885
|
+
windowsHide: true
|
|
18886
|
+
});
|
|
18887
|
+
return "killed";
|
|
18888
|
+
} catch (e) {
|
|
18889
|
+
const err = e;
|
|
18890
|
+
const said = `${err.stdout ?? ""}${err.stderr ?? ""}`.toLowerCase();
|
|
18891
|
+
if (said.includes("not found") || said.includes("没有找到") || err.status === 128) return "absent";
|
|
18892
|
+
return winIsAlive(pid) ? "denied" : "absent";
|
|
18893
|
+
}
|
|
18894
|
+
}
|
|
18895
|
+
//#endregion
|
|
18593
18896
|
//#region src/browser/process.ts
|
|
18594
18897
|
/**
|
|
18595
18898
|
* browser/process.ts — 槽位 Chrome 进程的启动、探活、回收
|
|
@@ -18606,6 +18909,19 @@ function macIsAlive(pid) {
|
|
|
18606
18909
|
* 3. **进程必须能被独立杀死**(`--user-data-dir` 指向槽位专属目录 ⇒ 可按目录精确认领,
|
|
18607
18910
|
* 不会误杀 user 自己的浏览器 —— 这是本次事故里我差点犯的第二个错)。
|
|
18608
18911
|
*/
|
|
18912
|
+
/**
|
|
18913
|
+
* 平台的人话名(只用于**回执文案**,⛔ 不参与任何判定)。
|
|
18914
|
+
*
|
|
18915
|
+
* 🔴 为什么要有它(v1.137.8 的一处小疏漏):原文案写的是
|
|
18916
|
+
* `isMac() ? 'macOS' : process.platform` ⇒ 在 mac 上印 `macOS`,
|
|
18917
|
+
* 在 Windows 上会印出裸的 `win32` —— 而 `win32` 是 Node 的内部标识,
|
|
18918
|
+
* 不是用户认得的名字(他会去搜"win32 是什么系统")。
|
|
18919
|
+
*/
|
|
18920
|
+
function platformLabel(platform = process.platform) {
|
|
18921
|
+
if (platform === "darwin") return "macOS";
|
|
18922
|
+
if (platform === "win32") return "Windows";
|
|
18923
|
+
return platform;
|
|
18924
|
+
}
|
|
18609
18925
|
function slotPaths(n, homeDir) {
|
|
18610
18926
|
const home = join(homeDir ?? slotsHomeDir(), slotLabel(n));
|
|
18611
18927
|
return {
|
|
@@ -18696,6 +19012,10 @@ function findChromeFor(platform, exists = existsSync, env = process.env) {
|
|
|
18696
19012
|
for (const p of macChromeCandidates(env)) if (exists(p)) return p;
|
|
18697
19013
|
return null;
|
|
18698
19014
|
}
|
|
19015
|
+
if (platform === "win32") {
|
|
19016
|
+
for (const p of winChromeCandidates(env)) if (exists(p)) return p;
|
|
19017
|
+
return null;
|
|
19018
|
+
}
|
|
18699
19019
|
for (const p of CHROME_CANDIDATES) if (exists(p)) return p;
|
|
18700
19020
|
return null;
|
|
18701
19021
|
}
|
|
@@ -18710,9 +19030,14 @@ function findChromeFor(platform, exists = existsSync, env = process.env) {
|
|
|
18710
19030
|
* 于是"平台分派"这件事**在 Linux 上永远读不出对错**。
|
|
18711
19031
|
* ⇒ 修法:把平台作为**参数**传进来,测试即可构造 `'darwin'` 走真分派。
|
|
18712
19032
|
* ⚠️ 生产调用点仍走默认参数(`process.platform`),行为零变化。
|
|
19033
|
+
*
|
|
19034
|
+
* 🆕 **2026-11-19 三向**(owner 令「做」/「在 win 上能用就行」):加 `win32` 一支。
|
|
19035
|
+
* Linux 与 darwin 两支**逐字未改**,只是 win32 分支插在它们之间。
|
|
18713
19036
|
*/
|
|
18714
19037
|
function chromeCandidatesFor(platform = process.platform) {
|
|
18715
|
-
|
|
19038
|
+
if (platform === "darwin") return MAC_CHROME_CANDIDATES;
|
|
19039
|
+
if (platform === "win32") return WIN_CHROME_CANDIDATES;
|
|
19040
|
+
return CHROME_CANDIDATES;
|
|
18716
19041
|
}
|
|
18717
19042
|
/** 生产包装(默认当前平台) */
|
|
18718
19043
|
function chromeCandidatesForMessage() {
|
|
@@ -18725,6 +19050,12 @@ function resolveDisplay(env = process.env, probe = {}, platform = process.platfo
|
|
|
18725
19050
|
backend: "cocoa",
|
|
18726
19051
|
detail: macDisplayDetail()
|
|
18727
19052
|
};
|
|
19053
|
+
if (platform === "win32") return {
|
|
19054
|
+
args: [],
|
|
19055
|
+
env: { ...env },
|
|
19056
|
+
backend: "native",
|
|
19057
|
+
detail: winDisplayDetail()
|
|
19058
|
+
};
|
|
18728
19059
|
const exists = probe.exists ?? existsSync;
|
|
18729
19060
|
const uid = process.getuid?.() ?? 1e3;
|
|
18730
19061
|
const runtimeDir = env.XDG_RUNTIME_DIR ?? `/run/user/${uid}`;
|
|
@@ -18786,10 +19117,26 @@ function resolveDisplay(env = process.env, probe = {}, platform = process.platfo
|
|
|
18786
19117
|
* ⚠️ 同样刻意**不用** `--disable-gpu`:Chrome 的 GPU 进程崩溃是这次实打实的死因,
|
|
18787
19118
|
* 但关掉 GPU 会让截图与 3D 页面拿到不同的渲染路径(软件光栅化)⇒ **取证条件变了**。
|
|
18788
19119
|
* 两条实现都保留 GPU(只用 `--no-sandbox` 解决 sandbox 问题),本实现跟随。
|
|
19120
|
+
*
|
|
19121
|
+
* 🔴🔴 **`--no-sandbox` 与 `--disable-dev-shm-usage` 是 Linux 专属**(v1.137.10,2026-11-19)。
|
|
19122
|
+
*
|
|
19123
|
+
* 这两条都**不是**"为了跨平台统一"的通用参数,它们的成因**只存在于 Linux**:
|
|
19124
|
+
* - `--no-sandbox`:容器内 Chrome 自身 sandbox 初始化失败(`Operation not permitted`)。
|
|
19125
|
+
* macOS / Windows 上**没有这个故障** ⇒ 在那里加上它等于**主动拆掉用户的浏览器沙箱**,
|
|
19126
|
+
* 把一个本可以正常工作的安全边界关掉,且**没有任何收益**。
|
|
19127
|
+
* - `--disable-dev-shm-usage`:容器 `/dev/shm` 只有 64MB。Windows 上没有 `/dev/shm` 这回事。
|
|
19128
|
+
*
|
|
19129
|
+
* ⇒ 判据:**平台专属的参数必须按平台给,⛔ 不能因为"统一"就全局带上。**
|
|
19130
|
+
* 这是本轨迹里"删值 = 删契约"的**反面**:这里是"**带值 = 造不存在的契约**"。
|
|
19131
|
+
* ⚠️ macOS 上原实现同样带着这两条(v1.137.8 没动它,那版只把启动跑通);
|
|
19132
|
+
* 本次**一并修正**:mac 上也不再带 `--no-sandbox`。
|
|
19133
|
+
* 🔴 **这是一处行为变更,不是纯重构** —— 它在 mac 上是**变好**(沙箱回来了),
|
|
19134
|
+
* 但属于"mac 上从未真机验过"的那一格里,所以只改参数、**不改那段诚实边界声明**。
|
|
18789
19135
|
*/
|
|
18790
|
-
function buildArgs(n, paths, display) {
|
|
19136
|
+
function buildArgs(n, paths, display, platform = process.platform) {
|
|
18791
19137
|
const { width, height } = SLOT_RESOLUTION(n);
|
|
18792
|
-
const disp = display ?? resolveDisplay();
|
|
19138
|
+
const disp = display ?? resolveDisplay(process.env, {}, platform);
|
|
19139
|
+
const linuxOnly = platform === "win32" || platform === "darwin" ? [] : ["--no-sandbox", "--disable-dev-shm-usage"];
|
|
18793
19140
|
return [
|
|
18794
19141
|
`--user-data-dir=${paths.profile}`,
|
|
18795
19142
|
`--remote-debugging-port=${slotPort(n)}`,
|
|
@@ -18798,8 +19145,7 @@ function buildArgs(n, paths, display) {
|
|
|
18798
19145
|
...disp.args,
|
|
18799
19146
|
"--no-first-run",
|
|
18800
19147
|
"--no-default-browser-check",
|
|
18801
|
-
|
|
18802
|
-
"--disable-dev-shm-usage",
|
|
19148
|
+
...linuxOnly,
|
|
18803
19149
|
"--disable-extensions",
|
|
18804
19150
|
`--force-device-scale-factor=1`,
|
|
18805
19151
|
`--window-size=${width},${height}`,
|
|
@@ -18821,7 +19167,7 @@ function launchSlot(n) {
|
|
|
18821
19167
|
if (!chrome) return {
|
|
18822
19168
|
ok: false,
|
|
18823
19169
|
pid: 0,
|
|
18824
|
-
detail: "未找到 Chrome/Chromium(平台:" + (
|
|
19170
|
+
detail: "未找到 Chrome/Chromium(平台:" + platformLabel() + ")。候选路径:" + chromeCandidatesForMessage().join(" / ") + "\n ⇒ 装一个 Chrome,或把路径加入上面任一位置。"
|
|
18825
19171
|
};
|
|
18826
19172
|
const paths = slotPaths(n);
|
|
18827
19173
|
if (!existsSync(paths.home)) mkdirSync(paths.home, { recursive: true });
|
|
@@ -18849,7 +19195,8 @@ function launchSlot(n) {
|
|
|
18849
19195
|
out,
|
|
18850
19196
|
out
|
|
18851
19197
|
],
|
|
18852
|
-
env: disp.env
|
|
19198
|
+
env: disp.env,
|
|
19199
|
+
windowsHide: true
|
|
18853
19200
|
});
|
|
18854
19201
|
child.unref();
|
|
18855
19202
|
const pid = child.pid ?? 0;
|
|
@@ -18875,6 +19222,7 @@ function launchSlot(n) {
|
|
|
18875
19222
|
*/
|
|
18876
19223
|
function portHolders(port) {
|
|
18877
19224
|
if (isMac()) return macPortHolders(port);
|
|
19225
|
+
if (isWin()) return winPortHolders(port);
|
|
18878
19226
|
const inodes = /* @__PURE__ */ new Set();
|
|
18879
19227
|
for (const f of ["/proc/net/tcp", "/proc/net/tcp6"]) {
|
|
18880
19228
|
let text;
|
|
@@ -18942,6 +19290,7 @@ function require_readlink(p) {
|
|
|
18942
19290
|
function findPidsByProfile(profileDir) {
|
|
18943
19291
|
if (!profileDir) return [];
|
|
18944
19292
|
if (isMac()) return macFindPidsByProfile(profileDir);
|
|
19293
|
+
if (isWin()) return winFindPidsByProfile(profileDir);
|
|
18945
19294
|
const pids = [];
|
|
18946
19295
|
let names;
|
|
18947
19296
|
try {
|
|
@@ -18971,6 +19320,7 @@ function findPidsByProfile(profileDir) {
|
|
|
18971
19320
|
*/
|
|
18972
19321
|
function sampleProcTree(pids, prev, now = Date.now()) {
|
|
18973
19322
|
if (isMac()) return macSample(pids, prev ?? null, now);
|
|
19323
|
+
if (isWin()) return winSample(pids, now);
|
|
18974
19324
|
let ticks = 0;
|
|
18975
19325
|
let rssKb = 0;
|
|
18976
19326
|
let anyAlive = false;
|
|
@@ -18998,11 +19348,13 @@ function sampleProcTree(pids, prev, now = Date.now()) {
|
|
|
18998
19348
|
cpuPct,
|
|
18999
19349
|
rssMb: rssKb / 1024,
|
|
19000
19350
|
at: now,
|
|
19001
|
-
ticks
|
|
19351
|
+
ticks,
|
|
19352
|
+
sampled: true
|
|
19002
19353
|
};
|
|
19003
19354
|
}
|
|
19004
19355
|
function killPidOutcome(pid, waitMs = 1500) {
|
|
19005
19356
|
if (!Number.isInteger(pid) || pid <= 1) return "absent";
|
|
19357
|
+
if (isWin()) return winKillTree(pid);
|
|
19006
19358
|
const existedBefore = pidIsAlive(pid);
|
|
19007
19359
|
try {
|
|
19008
19360
|
process.kill(pid, "SIGTERM");
|
|
@@ -19040,6 +19392,7 @@ function killPidOutcome(pid, waitMs = 1500) {
|
|
|
19040
19392
|
*/
|
|
19041
19393
|
function pidIsAlive(pid) {
|
|
19042
19394
|
if (isMac()) return macIsAlive(pid);
|
|
19395
|
+
if (isWin()) return winIsAlive(pid);
|
|
19043
19396
|
try {
|
|
19044
19397
|
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
19045
19398
|
const state = stat.slice(stat.lastIndexOf(")") + 2).charAt(0);
|
|
@@ -19120,10 +19473,17 @@ function destroySlot(n, pidHint = 0) {
|
|
|
19120
19473
|
*
|
|
19121
19474
|
* 🔴 `platform` 与 `macClaim` 都是**可注入参数**(与 {@link chromeCandidatesFor} 同一理由):
|
|
19122
19475
|
* 否则"分派真的发生了"这件事在 Linux 上**永远读不出对错** —— 删掉 darwin 分支测试也不会红。
|
|
19476
|
+
*
|
|
19477
|
+
* 🔴 **2026-11-19(v1.137.10)**:加 `win32` 一支。**同一族的缺陷不该有第三个版本** ——
|
|
19478
|
+
* 这次在加 Windows 支持时**主动**把它一起做了,而不是等 mac 那次的形态在 win 上重演一遍
|
|
19479
|
+
* (在 win 上重演的后果与 mac 完全相同:`destroySlot` 会印
|
|
19480
|
+
* 「台账那条记录是陈旧的或被污染」—— 一句在 win 上恒假的话)。
|
|
19481
|
+
* ⚠️ `winClaim` 同样是可注入参数,理由同上:⛔ 不能只靠"本机是 Linux"来验它。
|
|
19123
19482
|
*/
|
|
19124
|
-
function pidClaimsProfile(pid, profileDir, platform = process.platform, macClaim = macPidClaimsProfile) {
|
|
19483
|
+
function pidClaimsProfile(pid, profileDir, platform = process.platform, macClaim = macPidClaimsProfile, winClaim = winPidClaimsProfile) {
|
|
19125
19484
|
if (!Number.isInteger(pid) || pid <= 1) return false;
|
|
19126
19485
|
if (platform === "darwin") return macClaim(pid, profileDir);
|
|
19486
|
+
if (platform === "win32") return winClaim(pid, profileDir);
|
|
19127
19487
|
try {
|
|
19128
19488
|
return readFileSync(`/proc/${pid}/cmdline`, "utf8").includes(profileDir);
|
|
19129
19489
|
} catch {
|
|
@@ -20998,7 +21358,8 @@ function toView(root, n, lease, proc, now, policy) {
|
|
|
20998
21358
|
cpuPct: Number(proc.cpuPct.toFixed(1)),
|
|
20999
21359
|
rssMb: Math.round(proc.rssMb),
|
|
21000
21360
|
pid: lease?.pid ?? 0,
|
|
21001
|
-
nudges: lease?.nudges ?? 0
|
|
21361
|
+
nudges: lease?.nudges ?? 0,
|
|
21362
|
+
sampled: proc.sampled !== false
|
|
21002
21363
|
};
|
|
21003
21364
|
}
|
|
21004
21365
|
async function acquireSlot(input) {
|
|
@@ -21813,11 +22174,19 @@ function trajectoryNameFromCwd(root, cwd) {
|
|
|
21813
22174
|
const seg = cwd.slice(marker.length).split("/")[0] ?? "";
|
|
21814
22175
|
return seg.startsWith("_") ? "" : seg;
|
|
21815
22176
|
}
|
|
21816
|
-
/**
|
|
22177
|
+
/**
|
|
22178
|
+
* 一行渲染一个槽位(宽度克制:20 行要塞得进一屏)。
|
|
22179
|
+
*
|
|
22180
|
+
* 🔴 **`sampled === false` 时印 `—`,⛔ 不印 `0`**(2026-11-19,v1.137.10):
|
|
22181
|
+
* Windows 上刻意不采样(成本见 `process.ts` 的 win32 分支)⇒ 若照旧渲染 `cpu0% 内存0M`,
|
|
22182
|
+
* 那与「测了,确实很低」**完全同形**,而后者正是 2026-11-09 耗电事故里我们**没能及时发现**
|
|
22183
|
+
* 的那个读数。⇒ 判据:**「没测」必须与「测了是 0」在读数上分得开**(D253 家族)。
|
|
22184
|
+
*/
|
|
21817
22185
|
function row(v) {
|
|
21818
22186
|
const g = STATE_GLYPH[v.state] ?? "?";
|
|
21819
22187
|
const who = v.state === "free" ? "" : ` ${v.trajectory || v.sessionId || "(无归属)"}`;
|
|
21820
|
-
const
|
|
22188
|
+
const res = v.sampled ? `cpu${v.cpuPct}% 内存${v.rssMb}M` : "cpu— 内存—(本平台不采样)";
|
|
22189
|
+
const extra = v.state === "free" ? "" : ` 持${v.heldMin}m 闲${v.idleMin}m ${res}`;
|
|
21821
22190
|
const why = v.reasons.length > 0 ? `\n ↳ ${v.reasons.join(";")}` : "";
|
|
21822
22191
|
return ` ${g} ${v.label.padEnd(3)} :${v.port} ${v.resolution.padEnd(9)} ${v.state.padEnd(8)}${who}${extra}${why}`;
|
|
21823
22192
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shgroup/dsh-serenity-hooks",
|
|
3
|
-
"version": "1.137.
|
|
3
|
+
"version": "1.137.10",
|
|
4
4
|
"description": "宁静号 ACC harness(Native Cordis 插件)——给 DeepSeek Harness 装一个「AI 工作区」:14 个工具(container_fs/cognitive_trajectory/dashboard/container_git/msm/praxis/ontology/serenity-skill-creator/localstore/container_admin/browser_slot;另三个 im-bridge/diagram/reconstruct 按条件出现)+ 机械约束(安全模式/工作区围墙/密钥守卫/对外输出守卫)+ 认知轨迹日志与原地重建 + 网页登录入口/IM 桥/认知轨迹唤醒注册表 + 浏览器槽位管理(≤20 个独立 profile 的 Chrome 槽位,带占用归属与异常时向所属会话反向注入提醒)。轨迹声明 = 轨迹目录下的 trajectory.json(约束面 + 旧标识 uid + CRO 机器绑定 cro.host;旧名 trajectory-constraint.json / skiff.json 仍在读取族里);轨迹提示词 = 轨迹目录下的 PROMPT.md(一条轨迹「是谁、按什么章程工作」,所有轨迹通用,追加在容器层与质量层之后,不替换它们)。适配 DSH 0.1.7-rc.1 / 0.2.0-rc.1 / 0.2.1-rc.1(三档)。",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|