dsh-config-sync-plugin 0.3.72 → 0.3.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/client.js +2 -2
- package/index.js +8 -8
- package/package.json +1 -1
- package/runtime/apply.ps1 +2 -1
- package/tools/fix-session-cwd.mjs +200 -0
package/README.en.md
CHANGED
|
@@ -19,7 +19,7 @@ The package name is **`dsh-config-sync-plugin`** (the `dsh-config-sync` name on
|
|
|
19
19
|
|
|
20
20
|
| Way | Command / entry | Notes |
|
|
21
21
|
|---|---|---|
|
|
22
|
-
| npm | `npm i dsh-config-sync-plugin` | Use it as a dependency, or add `"dsh-config-sync-plugin": "^0.3.
|
|
22
|
+
| npm | `npm i dsh-config-sync-plugin` | Use it as a dependency, or add `"dsh-config-sync-plugin": "^0.3.74"` to the profile's `package.json` and run `pnpm i`. The name matches the `cordis.patch.yml` insert name and the install folder `dsh-config-sync-plugin-<ver>` |
|
|
23
23
|
| Marketplace | client → Plugins → Discover → “Config Sync” | Installs from the git commit registered in `market-index.json` |
|
|
24
24
|
| Offline tgz | `releases\dsh-config-sync-plugin-<ver>.tgz` | GitHub Release asset for air-gapped machines; the panel's Update button downloads the same file |
|
|
25
25
|
|
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ DSH 插件:左侧栏多一行「配置同步」,点开是整页体检面板
|
|
|
15
15
|
|
|
16
16
|
| 方式 | 命令 / 入口 | 说明 |
|
|
17
17
|
|---|---|---|
|
|
18
|
-
| npm | `npm i dsh-config-sync-plugin` | 适合把它当依赖管理,或在 profile 的 `package.json` 里写 `"dsh-config-sync-plugin": "^0.3.
|
|
18
|
+
| npm | `npm i dsh-config-sync-plugin` | 适合把它当依赖管理,或在 profile 的 `package.json` 里写 `"dsh-config-sync-plugin": "^0.3.74"` 后 `pnpm i`;包名与 `cordis.patch.yml` 里注册的名字、插件目录 `dsh-config-sync-plugin-<ver>` 一致 |
|
|
19
19
|
| 插件市场 | 客户端「插件市场 → 发现」搜「配置同步」 | 走 `market-index.json` 登记的 git 提交,版本与 README 对齐 |
|
|
20
20
|
| 离线 tgz | `releases\dsh-config-sync-plugin-<ver>.tgz` | GitHub Release 附件,适合内网机器;面板「更新」按钮下载的也是它 |
|
|
21
21
|
|
package/client.js
CHANGED
|
@@ -3212,7 +3212,7 @@ window.__ModuleLoader__.load({
|
|
|
3212
3212
|
} catch { /* 插件路由还没起好就静默跳过,面板里仍会显示同一结果 */ }
|
|
3213
3213
|
}
|
|
3214
3214
|
|
|
3215
|
-
// ==================== 0.3.
|
|
3215
|
+
// ==================== 0.3.74:会话缓存守卫(客户端半身) ====================
|
|
3216
3216
|
// 宿主侧没法拦 agent-busy(它是 sessionController 内部抛的 RemoteError),
|
|
3217
3217
|
// 所以在这里包一层 sessions.create:撞上就上报、清缓存、去掉旧 sessionId 重试一次。
|
|
3218
3218
|
const BUSY_RE = /session\/agent-busy|is owned by subagent routing/i
|
|
@@ -3309,7 +3309,7 @@ window.__ModuleLoader__.load({
|
|
|
3309
3309
|
// 界面还没完全起来时 fetch 可能扑空,所以稍等一会儿再查;只查一次,不轮询。
|
|
3310
3310
|
try { setTimeout(announceLastResult, 1500) } catch { }
|
|
3311
3311
|
|
|
3312
|
-
// 0.3.
|
|
3312
|
+
// 0.3.74:给客户端的 sessions 服务套上 agent-busy 自愈钩子。
|
|
3313
3313
|
// 拿不到 sessions(老版本宿主 / 服务还没起来)就静默跳过,面板功能不受影响。
|
|
3314
3314
|
try {
|
|
3315
3315
|
if (typeof ctx.get === 'function') {
|
package/index.js
CHANGED
|
@@ -24,7 +24,7 @@ import { closeSync, existsSync, mkdirSync, mkdtempSync, openSync, readdirSync, r
|
|
|
24
24
|
import { homedir, hostname, tmpdir } from 'node:os'
|
|
25
25
|
import { basename, dirname, isAbsolute, join, relative } from 'node:path'
|
|
26
26
|
import { fileURLToPath } from 'node:url'
|
|
27
|
-
// 0.3.
|
|
27
|
+
// 0.3.74:会话投影缓存守卫(agent-busy 自动清缓存,让缓存按日志重建)
|
|
28
28
|
import { runGuard, clearSessionCaches, appendGuardLog } from './runtime/tools/session-cache-guard.mjs'
|
|
29
29
|
|
|
30
30
|
const pluginRequire = createRequire(import.meta.url)
|
|
@@ -39,7 +39,7 @@ const ROUTE_CONFLICT = '/config-sync/conflict'
|
|
|
39
39
|
// 0.3.30:跨机路径确认(每台机器把自己的根目录报给插件一次)与问题反馈(落仓库 feedback\ 再推 GitHub)
|
|
40
40
|
const ROUTE_ROOTS = '/config-sync/roots'
|
|
41
41
|
const ROUTE_FEEDBACK = '/config-sync/feedback'
|
|
42
|
-
// 0.3.
|
|
42
|
+
// 0.3.74:会话投影缓存守卫(GET 只读扫描 / POST scan|sweep|clear|busy)
|
|
43
43
|
const ROUTE_CACHE_GUARD = '/config-sync/cache-guard'
|
|
44
44
|
const SETTINGS_NAME = 'dsh-config-sync.json'
|
|
45
45
|
/** 本插件自己的安装目录(插件根,含 runtime\ 与 index.js)—— 插件备份还原时的目标。 */
|
|
@@ -237,7 +237,7 @@ function readSettings(dshHome) {
|
|
|
237
237
|
// 同步完成重启前把 DSH 的「默认工作区」校正到谁:
|
|
238
238
|
// 留空 = 自动找名字/路径是 default-workspace 的工作区;可填工作区路径或 ID;填 off 关闭。
|
|
239
239
|
defaultWorkspace: typeof raw.defaultWorkspace === 'string' ? raw.defaultWorkspace : '',
|
|
240
|
-
// 0.3.
|
|
240
|
+
// 0.3.74:会话投影缓存守卫,默认开。关掉后既不定时扫描,也不理会客户端的 agent-busy 上报。
|
|
241
241
|
cacheGuard: raw.cacheGuard !== false,
|
|
242
242
|
}
|
|
243
243
|
}
|
|
@@ -1180,7 +1180,7 @@ async function inspect(config) {
|
|
|
1180
1180
|
const pending = c.items.filter((it) => !it.choice).length
|
|
1181
1181
|
return { exists: true, total: c.items.length, pending, at: c.at || '', backupDir: c.backupDir || '' }
|
|
1182
1182
|
})(),
|
|
1183
|
-
// 0.3.
|
|
1183
|
+
// 0.3.74:会话投影缓存守卫状态(面板「会话缓存守卫」卡用)
|
|
1184
1184
|
cacheGuard: cacheGuardSnapshot(config),
|
|
1185
1185
|
}
|
|
1186
1186
|
}
|
|
@@ -1962,7 +1962,7 @@ async function handleSettings(req, res, config) {
|
|
|
1962
1962
|
const autoPull = has('autoPull') ? body.autoPull === true : before.autoPull
|
|
1963
1963
|
const autoPush = has('autoPush') ? body.autoPush === true : before.autoPush
|
|
1964
1964
|
const autoRestart = has('autoRestart') ? body.autoRestart !== false : before.autoRestart
|
|
1965
|
-
// 0.3.
|
|
1965
|
+
// 0.3.74:会话投影缓存守卫开关,字段没给就沿用原值
|
|
1966
1966
|
const cacheGuard = has('cacheGuard') ? body.cacheGuard !== false : before.cacheGuard
|
|
1967
1967
|
|
|
1968
1968
|
if (!has('repoPath') && !has('remoteUrl') && !has('sshKey') &&
|
|
@@ -4022,7 +4022,7 @@ function newIssueUrl(repo, title, text, config) {
|
|
|
4022
4022
|
return `https://github.com/${repo}/issues/new?${q}`
|
|
4023
4023
|
}
|
|
4024
4024
|
|
|
4025
|
-
// ============================ 0.3.
|
|
4025
|
+
// ============================ 0.3.74:会话投影缓存守卫 ============================
|
|
4026
4026
|
// 背景:UI 点「新建会话」时会挑一条 summary.blank===true 的会话复用,挑中了 origin=subagent
|
|
4027
4027
|
// 的子会话就会被宿主拒绝(session/agent-busy:is owned by subagent routing)。
|
|
4028
4028
|
// 起因是投影缓存停在 session/created 那一刻(blank=true、seq=-1),而会话日志早就推进过,
|
|
@@ -4430,7 +4430,7 @@ export function apply(ctx, config) {
|
|
|
4430
4430
|
path: ROUTE_FEEDBACK,
|
|
4431
4431
|
handler: withLang(handleFeedback),
|
|
4432
4432
|
}))
|
|
4433
|
-
// 0.3.
|
|
4433
|
+
// 0.3.74:会话投影缓存守卫(GET 只读扫描 / POST scan|sweep|clear|busy)
|
|
4434
4434
|
scoped.effect(() => scoped.webServer.register({
|
|
4435
4435
|
kind: 'exact',
|
|
4436
4436
|
path: ROUTE_CACHE_GUARD,
|
|
@@ -4443,7 +4443,7 @@ export function apply(ctx, config) {
|
|
|
4443
4443
|
// status 不用再为首屏那 2.5 秒的 ls-remote 埋单。
|
|
4444
4444
|
typeof setTimeout === 'function' ? setTimeout(() => { try { warmRemoteProbe(config) } catch { } }, 300) : null
|
|
4445
4445
|
installQuitWhenAllWindowsClosed(ctx)
|
|
4446
|
-
// 0.3.
|
|
4446
|
+
// 0.3.74:会话投影缓存守卫的定时扫描(默认开,设置里可关)
|
|
4447
4447
|
scheduleCacheGuard(config)
|
|
4448
4448
|
} catch (error) {
|
|
4449
4449
|
// 不抛:缺 webServer 的 profile 里这个插件静默不激活即可
|
package/package.json
CHANGED
package/runtime/apply.ps1
CHANGED
|
@@ -900,7 +900,8 @@ $localPluginMap = [ordered]@{
|
|
|
900
900
|
'dsh-purge-1.4.0' = 'dsh-purge'
|
|
901
901
|
'dsh-config-sync-plugin-0.3.69' = 'dsh-config-sync-plugin'
|
|
902
902
|
'dsh-config-sync-plugin-0.3.70' = 'dsh-config-sync-plugin'
|
|
903
|
-
'dsh-config-sync-plugin-0.3.
|
|
903
|
+
'dsh-config-sync-plugin-0.3.74' = 'dsh-config-sync-plugin'
|
|
904
|
+
'dsh-config-sync-plugin-0.3.73' = 'dsh-config-sync-plugin'
|
|
904
905
|
'dsh-config-sync-plugin-0.3.68' = 'dsh-config-sync-plugin'
|
|
905
906
|
'dsh-config-sync-plugin-0.3.67' = 'dsh-config-sync-plugin'
|
|
906
907
|
'dsh-config-sync-plugin-0.3.66' = 'dsh-config-sync-plugin'
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// 修正会话日志头部的 cwd —— 跨机还原会话的必要一步。
|
|
2
|
+
//
|
|
3
|
+
// 背景:DSH 把会话存在 <home>\sessions\<编码目录名>\<会话>\session.v4.jsonl.zstd,
|
|
4
|
+
// 目录名由「工作区绝对路径」编码而来;同时会话文件第一行 header 里还存着 cwd。
|
|
5
|
+
// 换机器/换盘后目录名按本机路径重新编码,但文件里的 cwd 仍是源机路径,
|
|
6
|
+
// DSH 校验两者不一致 → 判定 corrupt session log → sessionController 起不来 →
|
|
7
|
+
// 工作区/会话列表全空、界面报 "web boot: 1 entry did not activate"。
|
|
8
|
+
//
|
|
9
|
+
// ⚠ 关键:这些 .zstd 是**追加写入的多帧文件**(一个事件一帧,几百上千帧),
|
|
10
|
+
// 而 Node 的 zstdDecompressSync / createZstdDecompress 都**只解第一帧**。
|
|
11
|
+
// 若按"解压全文 → 改 header → 整文件重新压缩"来做,第二帧起的内容会被全部丢掉
|
|
12
|
+
// (文件从 900 KB 缩成 200 字节,会话只剩一个空壳)。所以这里只重写第一帧:
|
|
13
|
+
// 用二分找出第一帧的结束偏移,替换该帧,后面的字节原样保留。
|
|
14
|
+
//
|
|
15
|
+
// 用法:node fix-session-cwd.mjs <spec.json> [结果输出文件]
|
|
16
|
+
// spec.json = { "home": "<DSH home>", "dirs": { "<编码目录名>": "<本机工作区路径>" } }
|
|
17
|
+
// 只改 header 的 cwd,正文一帧都不动;已正确则跳过(幂等)。
|
|
18
|
+
//
|
|
19
|
+
// 第三个参数是给「客户端自带 Electron 当 node 跑」(ELECTRON_RUN_AS_NODE=1)用的:
|
|
20
|
+
// Electron 是 GUI 子系统程序,stdout 抓不到,结果必须落到文件里由 PowerShell 读。
|
|
21
|
+
|
|
22
|
+
import fs from 'node:fs';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
import zlib from 'node:zlib';
|
|
25
|
+
|
|
26
|
+
const specPath = process.argv[2];
|
|
27
|
+
const outPath = process.argv[3] || '';
|
|
28
|
+
if (!specPath) {
|
|
29
|
+
console.error('用法:node fix-session-cwd.mjs <spec.json> [out.json]');
|
|
30
|
+
process.exit(2);
|
|
31
|
+
}
|
|
32
|
+
const emit = (obj) => {
|
|
33
|
+
const text = JSON.stringify(obj);
|
|
34
|
+
if (outPath) {
|
|
35
|
+
try { fs.writeFileSync(outPath, text, 'utf8'); } catch { console.log(text); }
|
|
36
|
+
} else {
|
|
37
|
+
console.log(text);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
if (typeof zlib.zstdDecompressSync !== 'function' || typeof zlib.zstdCompressSync !== 'function') {
|
|
41
|
+
console.error(`当前 Node ${process.version} 不支持 zstd,无法修正会话头部`);
|
|
42
|
+
process.exit(2);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const spec = JSON.parse(fs.readFileSync(specPath, 'utf8'));
|
|
46
|
+
const home = String(spec.home || '').replace(/\//g, '\\').replace(/\\+$/, '');
|
|
47
|
+
const dirs = spec.dirs || {};
|
|
48
|
+
const sessionsRoot = path.join(home, 'sessions');
|
|
49
|
+
|
|
50
|
+
const result = { fixed: 0, alreadyOk: 0, missing: 0, failed: [] };
|
|
51
|
+
|
|
52
|
+
/** 第一帧解出来的文本(多帧文件也只返回第一帧)。 */
|
|
53
|
+
function decodeFirstFrame(buf) {
|
|
54
|
+
return zlib.zstdDecompressSync(buf);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 首帧是否带 zstd content checksum(帧头描述符 FHD 的 bit2)。 */
|
|
58
|
+
function frameHasChecksum(buf) {
|
|
59
|
+
return buf.length > 4 && ((buf.readUInt8(4) >> 2) & 1) === 1;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const ZSTD_MAGIC = [0x28, 0xb5, 0x2f, 0xfd];
|
|
63
|
+
const isFrameMagic = (b, i) => b[i] === 0x28 && b[i + 1] === 0xb5 && b[i + 2] === 0x2f && b[i + 3] === 0xfd;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 第一帧的结束偏移(即下一帧的起点)。
|
|
67
|
+
* 二分得到的是「最小可解前缀」——解压器只要能出数据就返回成功,
|
|
68
|
+
* 所以带 content checksum 的帧会算短 4 字节(checksum 那段被当成可省)。
|
|
69
|
+
* 不算这 4 字节就会把旧 checksum 留在文件里,DSH 读第二帧时报
|
|
70
|
+
* "corrupt Zstandard session log: invalid frame magic at byte N",连新建会话都失败。
|
|
71
|
+
* 因此:先按 FHD 判断补 4 字节,再以「下一帧帧头」为准做一次校正。
|
|
72
|
+
*/
|
|
73
|
+
function firstFrameEnd(buf, expected) {
|
|
74
|
+
let lo = 1;
|
|
75
|
+
let hi = buf.length;
|
|
76
|
+
while (lo < hi) {
|
|
77
|
+
const mid = (lo + hi) >> 1;
|
|
78
|
+
let ok = false;
|
|
79
|
+
try {
|
|
80
|
+
ok = decodeFirstFrame(buf.subarray(0, mid)).equals(expected);
|
|
81
|
+
} catch {
|
|
82
|
+
ok = false;
|
|
83
|
+
}
|
|
84
|
+
if (ok) hi = mid;
|
|
85
|
+
else lo = mid + 1;
|
|
86
|
+
}
|
|
87
|
+
const min = lo;
|
|
88
|
+
const withChecksum = min + (frameHasChecksum(buf) ? 4 : 0);
|
|
89
|
+
// 帧尾就落在 min / min+4 / min+4 后面紧跟帧头,三种情况都认;否则退回 min+4
|
|
90
|
+
if (min >= buf.length || isFrameMagic(buf, min)) return min;
|
|
91
|
+
if (withChecksum >= buf.length || isFrameMagic(buf, withChecksum)) return withChecksum;
|
|
92
|
+
return Math.min(withChecksum, buf.length);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const projSpec = spec.projcache || {};
|
|
96
|
+
if (!fs.existsSync(sessionsRoot) && !Object.keys(projSpec).length) {
|
|
97
|
+
emit(result);
|
|
98
|
+
process.exit(0);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for (const [dirName, cwd] of Object.entries(dirs)) {
|
|
102
|
+
const dirPath = path.join(sessionsRoot, dirName);
|
|
103
|
+
if (!fs.existsSync(dirPath)) { result.missing++; continue; }
|
|
104
|
+
for (const entry of fs.readdirSync(dirPath, { withFileTypes: true })) {
|
|
105
|
+
if (!entry.isDirectory()) continue;
|
|
106
|
+
const file = path.join(dirPath, entry.name, 'session.v4.jsonl.zstd');
|
|
107
|
+
if (!fs.existsSync(file)) continue;
|
|
108
|
+
try {
|
|
109
|
+
const buf = fs.readFileSync(file);
|
|
110
|
+
const first = decodeFirstFrame(buf);
|
|
111
|
+
const nl = first.indexOf(0x0a);
|
|
112
|
+
if (nl < 0) throw new Error('第一帧里没有 header 行');
|
|
113
|
+
const header = JSON.parse(first.subarray(0, nl).toString('utf8'));
|
|
114
|
+
if (header.cwd === cwd) { result.alreadyOk++; continue; }
|
|
115
|
+
|
|
116
|
+
const text = first.toString('utf8');
|
|
117
|
+
// 只替换首帧里的 cwd 值,首帧其余文本(含可能的追加行)原样保留
|
|
118
|
+
const newText = text.replace(/"cwd"\s*:\s*("(?:[^"\\]|\\.)*")/, `"cwd":${JSON.stringify(cwd)}`);
|
|
119
|
+
const newFrame = zlib.zstdCompressSync(Buffer.from(newText, 'utf8'));
|
|
120
|
+
let end = firstFrameEnd(buf, first);
|
|
121
|
+
// 历史写坏的文件里,第一帧边界之后还残留着 4 字节旧 checksum,
|
|
122
|
+
// 真正可用的下一帧在后面的帧头处——顺手把这段残缺尾巴丢掉(自愈)。
|
|
123
|
+
if (end < buf.length && !isFrameMagic(buf, end)) {
|
|
124
|
+
for (let k = end; k < Math.min(end + 9, buf.length); k++) {
|
|
125
|
+
if (isFrameMagic(buf, k)) { end = k; break; }
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const rest = buf.subarray(end);
|
|
129
|
+
if (rest.length && !isFrameMagic(rest, 0)) {
|
|
130
|
+
throw new Error(`第一帧边界(${end})之后不是 zstd 帧头,跳过以免写坏后续帧`);
|
|
131
|
+
}
|
|
132
|
+
const rebuilt = Buffer.concat([newFrame, rest]);
|
|
133
|
+
if (rebuilt.length + 64 < buf.length) {
|
|
134
|
+
throw new Error(`重写后变小(${buf.length} → ${rebuilt.length}),正文可能被丢,已跳过`);
|
|
135
|
+
}
|
|
136
|
+
// 先自检、后落盘:任何一项不过都不写文件
|
|
137
|
+
const preText = decodeFirstFrame(rebuilt).toString('utf8');
|
|
138
|
+
const preHeader = JSON.parse(preText.slice(0, preText.indexOf('\n')));
|
|
139
|
+
if (preHeader.cwd !== cwd) throw new Error('自检失败:header.cwd 未生效,未写入');
|
|
140
|
+
// 自检看的是「新首帧之后」,所以用新帧长度,不是旧文件的 end
|
|
141
|
+
if (rebuilt.length > newFrame.length && !isFrameMagic(rebuilt, newFrame.length)) {
|
|
142
|
+
throw new Error(`自检失败:第 ${newFrame.length} 字节处不是下一帧帧头,未写入`);
|
|
143
|
+
}
|
|
144
|
+
fs.writeFileSync(file, rebuilt);
|
|
145
|
+
|
|
146
|
+
const verifyText = decodeFirstFrame(fs.readFileSync(file)).toString('utf8');
|
|
147
|
+
const verifyHeader = JSON.parse(verifyText.slice(0, verifyText.indexOf('\n')));
|
|
148
|
+
if (verifyHeader.cwd !== cwd) throw new Error('写回后校验失败:header.cwd 未生效');
|
|
149
|
+
result.fixed++;
|
|
150
|
+
} catch (e) {
|
|
151
|
+
result.failed.push({ file, error: e.message });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// projcache(<home>\storages\session_projcache\sessions\<sessionId>.json)里也存着
|
|
157
|
+
// record.identity.cwd。它是纯 JSON,可以安全地只替换那一个字符串,其余字节不动。
|
|
158
|
+
// 不换的话,B 机打开这些会话时 cwd 仍指 A 机的目录,历史对话回不到正确工作区。
|
|
159
|
+
const projMap = projSpec;
|
|
160
|
+
const projDir = path.join(home, 'storages', 'session_projcache', 'sessions');
|
|
161
|
+
result.projFixed = 0;
|
|
162
|
+
result.projAlreadyOk = 0;
|
|
163
|
+
result.projFailed = [];
|
|
164
|
+
if (Object.keys(projMap).length && fs.existsSync(projDir)) {
|
|
165
|
+
for (const entry of fs.readdirSync(projDir, { withFileTypes: true })) {
|
|
166
|
+
if (!entry.isFile() || !entry.name.toLowerCase().endsWith('.json')) continue;
|
|
167
|
+
const sid = entry.name.slice(0, -5);
|
|
168
|
+
const target = projMap[sid];
|
|
169
|
+
if (!target) continue;
|
|
170
|
+
const file = path.join(projDir, entry.name);
|
|
171
|
+
try {
|
|
172
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
173
|
+
const doc = JSON.parse(text);
|
|
174
|
+
const cur = doc && doc.record && doc.record.identity && doc.record.identity.cwd;
|
|
175
|
+
if (typeof cur !== 'string') continue;
|
|
176
|
+
if (cur === target) { result.projAlreadyOk++; continue; }
|
|
177
|
+
// 只换第一处出现(record.identity.cwd 在文件靠前位置),其余字节原样保留
|
|
178
|
+
const re = /"cwd"\s*:\s*("(?:[^"\\]|\\.)*")/;
|
|
179
|
+
const m = re.exec(text);
|
|
180
|
+
if (!m) throw new Error('找不到 identity.cwd 的字面位置');
|
|
181
|
+
const from = m[1];
|
|
182
|
+
const to = JSON.stringify(target);
|
|
183
|
+
if (from === to) { result.projAlreadyOk++; continue; }
|
|
184
|
+
const next = text.slice(0, m.index) + '"cwd": ' + to + text.slice(m.index + m[0].length);
|
|
185
|
+
// 只在「内容真的没变」时算已正确。早先这里用 next.length === text.length 判等,
|
|
186
|
+
// 两台机器路径长度相同时(D:\DeepSeekHarness → E:\DeepSeekHarness 这种)会把
|
|
187
|
+
// 该改的文件误判成「本就正确」,cwd 就留在源机路径上(隔离环境实测抓到)。
|
|
188
|
+
if (next === text) { result.projAlreadyOk++; continue; }
|
|
189
|
+
fs.writeFileSync(file, next, 'utf8');
|
|
190
|
+
const check = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
191
|
+
if (check.record.identity.cwd !== target) throw new Error('写回后校验失败');
|
|
192
|
+
result.projFixed++;
|
|
193
|
+
} catch (e) {
|
|
194
|
+
result.projFailed.push({ file, error: e.message });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
emit(result);
|
|
200
|
+
process.exit((result.failed.length || result.projFailed.length) ? 1 : 0);
|