dsh-config-sync-plugin 0.3.68 → 0.3.70
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 +17 -180
- package/README.md +19 -171
- package/client.js +58 -31
- package/index.js +9 -9
- package/package.json +1 -1
- package/runtime/apply.ps1 +37 -6
- package/runtime/capture.ps1 +20 -1
- package/runtime/dsh-sync-common.ps1 +153 -1
- package/runtime/post-exit.ps1 +2 -0
- package/runtime/tools/bili-state.mjs +501 -0
- package/tools/bili-state.mjs +501 -0
- package/tools/selftest.mjs +67 -0
|
@@ -31,6 +31,8 @@ function Die($m) { Write-Host " [X] $m" -ForegroundColor Red; exit 1 }
|
|
|
31
31
|
# skipped 被规则或占用跳过(不是失败,但用户该知道)
|
|
32
32
|
# failed 写入失败、被远端拒绝等真失败
|
|
33
33
|
# history 历史提交里已存在的超限文件(远端必然拒收,必须改写历史或换仓库)
|
|
34
|
+
# plugin-missing 插件没装在本机(拉取只还原配置,不装依赖;装完要再拉一次)
|
|
35
|
+
# context-state billion-context 的压缩状态没跟着过来(该会话可能报上下文超长)
|
|
34
36
|
$script:SyncNoticeItems = New-Object 'System.Collections.ArrayList'
|
|
35
37
|
$script:SyncNoticeHome = $null
|
|
36
38
|
$script:SyncNoticeMode = ''
|
|
@@ -50,12 +52,15 @@ function Read-SyncNotices([string]$dshHome) {
|
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
function Save-SyncNotices {
|
|
55
|
+
# 没有明确的 home 就不写:Get-SyncNoticesPath 会回落到 %USERPROFILE%\.dsh,
|
|
56
|
+
# 于是一次「没有上下文」的调用就会把本机真实清单改掉(隔离测试/被 sync 调起的子进程都踩过)。
|
|
57
|
+
if (-not $script:SyncNoticeHome) { return }
|
|
53
58
|
try {
|
|
54
59
|
$p = Get-SyncNoticesPath $script:SyncNoticeHome
|
|
55
60
|
$dir = Split-Path -Parent $p
|
|
56
61
|
if ($dir -and -not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
|
|
57
62
|
$byKind = [ordered]@{}
|
|
58
|
-
foreach ($k in @('too-large', 'history', 'skipped', 'failed')) {
|
|
63
|
+
foreach ($k in @('too-large', 'history', 'skipped', 'failed', 'plugin-missing', 'context-state')) {
|
|
59
64
|
$byKind[$k] = @($script:SyncNoticeItems | Where-Object { $_.kind -eq $k }).Count
|
|
60
65
|
}
|
|
61
66
|
$obj = [ordered]@{
|
|
@@ -69,6 +74,16 @@ function Save-SyncNotices {
|
|
|
69
74
|
} catch { }
|
|
70
75
|
}
|
|
71
76
|
|
|
77
|
+
# 被 sync 调起的子进程(capture / apply)带 -NoResetNotices:它们不能清空清单,
|
|
78
|
+
# 但**必须**建立 home/mode 上下文 —— 否则 Add-SyncNotice 会回落到 %USERPROFILE%\.dsh,
|
|
79
|
+
# 把本该写到 -DshHome 的条目写进别人家的清单里。
|
|
80
|
+
function Set-SyncNoticeContext([string]$dshHome, [string]$mode) {
|
|
81
|
+
if (-not $dshHome) { return }
|
|
82
|
+
$script:SyncNoticeHome = $dshHome
|
|
83
|
+
if ($mode) { $script:SyncNoticeMode = $mode }
|
|
84
|
+
$script:SyncNoticeLoaded = $true
|
|
85
|
+
}
|
|
86
|
+
|
|
72
87
|
# 每个动作开始时清空。被 sync 调起的子脚本(capture)要传 -NoResetNotices,
|
|
73
88
|
# 否则它一 Reset 就把父进程已经攒下的条目抹掉。
|
|
74
89
|
function Reset-SyncNotices([string]$dshHome, [string]$mode) {
|
|
@@ -82,6 +97,7 @@ function Reset-SyncNotices([string]$dshHome, [string]$mode) {
|
|
|
82
97
|
function Add-SyncNotice {
|
|
83
98
|
param([string]$Kind = 'skipped', [string]$Path = '', [string]$Reason = '', [long]$Bytes = 0)
|
|
84
99
|
if (-not $Path) { return }
|
|
100
|
+
if (-not $script:SyncNoticeHome) { return }
|
|
85
101
|
# 先吃进磁盘上别的进程已经写好的条目(capture 与 sync 是两个进程)
|
|
86
102
|
if (-not $script:SyncNoticeLoaded) {
|
|
87
103
|
$existing = Read-SyncNotices $script:SyncNoticeHome
|
|
@@ -108,6 +124,8 @@ function Get-SyncNoticesSummary($notices) {
|
|
|
108
124
|
if ($bk.history -gt 0) { $parts += ("{0} 个历史提交里的超限文件" -f $bk.history) }
|
|
109
125
|
if ($bk.skipped -gt 0) { $parts += ("{0} 个被规则跳过" -f $bk.skipped) }
|
|
110
126
|
if ($bk.failed -gt 0) { $parts += ("{0} 个写入/推送失败" -f $bk.failed) }
|
|
127
|
+
if ($bk.'plugin-missing' -gt 0) { $parts += ("{0} 个插件没装" -f $bk.'plugin-missing') }
|
|
128
|
+
if ($bk.'context-state' -gt 0) { $parts += ("{0} 个会话缺压缩状态" -f $bk.'context-state') }
|
|
111
129
|
return ($parts -join '、')
|
|
112
130
|
}
|
|
113
131
|
|
|
@@ -1141,3 +1159,137 @@ function Write-SessionLogGuardReport($Guard, [string]$DshHome, [string]$Verb) {
|
|
|
1141
1159
|
}
|
|
1142
1160
|
}
|
|
1143
1161
|
|
|
1162
|
+
|
|
1163
|
+
# ── billion-context 压缩状态跨机搬运 ───────────────────────────────────────
|
|
1164
|
+
# 为什么需要:DSH 原生 compaction 被 billion-context 接管(compaction gate 把
|
|
1165
|
+
# dsh-compaction-basic / dsh-command-compact disabled 掉),真正的压缩记录存在
|
|
1166
|
+
# bili 自己的 SQLite(…\billion-context\sessions\index.db)里,不在 .dsh 下。
|
|
1167
|
+
# 只同步会话日志、不同步这份状态,另一台机器就会把未压缩的全量 surface 发给
|
|
1168
|
+
# provider,报 CONTEXT_WINDOW_EXCEEDED。这里按会话导出/导入 blocks+refs+content。
|
|
1169
|
+
|
|
1170
|
+
function Get-BiliStateToolPath([string]$scriptRoot, [string]$repoRoot) {
|
|
1171
|
+
$bases = @()
|
|
1172
|
+
if ($scriptRoot) {
|
|
1173
|
+
$bases += (Join-Path $scriptRoot 'tools')
|
|
1174
|
+
if ((Split-Path -Leaf $scriptRoot) -ne 'runtime') { $bases += (Join-Path $scriptRoot 'runtime\tools') }
|
|
1175
|
+
}
|
|
1176
|
+
if ($repoRoot) {
|
|
1177
|
+
$bases += (Join-Path $repoRoot 'tools')
|
|
1178
|
+
$bases += (Join-Path $repoRoot 'runtime\tools')
|
|
1179
|
+
}
|
|
1180
|
+
if ($MyInvocation.MyCommand.Path) {
|
|
1181
|
+
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
1182
|
+
$bases += (Join-Path $here 'tools')
|
|
1183
|
+
}
|
|
1184
|
+
foreach ($b in $bases) {
|
|
1185
|
+
if (-not $b) { continue }
|
|
1186
|
+
$p = Join-Path $b 'bili-state.mjs'
|
|
1187
|
+
if (Test-Path -LiteralPath $p) { return $p }
|
|
1188
|
+
}
|
|
1189
|
+
return $null
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
# 形状仿 Invoke-SessionLogGuard:spec/结果都走临时文件,执行器可能是 Electron GUI 子系统。
|
|
1193
|
+
function Invoke-BiliStateSync {
|
|
1194
|
+
param(
|
|
1195
|
+
[ValidateSet('export', 'import')][string]$Mode,
|
|
1196
|
+
[string]$RepoDir,
|
|
1197
|
+
[object[]]$Sessions,
|
|
1198
|
+
[string]$AppRoot,
|
|
1199
|
+
[string]$ScriptRoot,
|
|
1200
|
+
[string]$RepoRoot,
|
|
1201
|
+
[string]$BiliDir
|
|
1202
|
+
)
|
|
1203
|
+
$res = [ordered]@{ Ok = $true; Mode = $Mode; Exported = @(); Imported = @(); Missing = @(); Failed = @(); Skipped = $true; Reason = ''; NoBili = $false; BiliDir = ''; Bytes = 0 }
|
|
1204
|
+
if (-not $RepoDir) { $res.Reason = '没有指定压缩状态目录'; return $res }
|
|
1205
|
+
$tool = Get-BiliStateToolPath $ScriptRoot $RepoRoot
|
|
1206
|
+
if (-not $tool) { $res.Reason = '找不到 tools\bili-state.mjs'; return $res }
|
|
1207
|
+
$runner = Get-HostNodeRunner $AppRoot
|
|
1208
|
+
if (-not $runner) { $res.Reason = '这台机器既没有 Node 也找不到客户端本体'; return $res }
|
|
1209
|
+
|
|
1210
|
+
$stamp = Get-Date -Format 'yyyyMMddHHmmssfff'
|
|
1211
|
+
$specPath = Join-Path ([IO.Path]::GetTempPath()) ('dsh-bilistate-spec-{0}-{1}.json' -f $stamp, $PID)
|
|
1212
|
+
$outPath = Join-Path ([IO.Path]::GetTempPath()) ('dsh-bilistate-out-{0}-{1}.json' -f $stamp, $PID)
|
|
1213
|
+
$spec = [ordered]@{
|
|
1214
|
+
mode = $Mode
|
|
1215
|
+
repoDir = [string]$RepoDir
|
|
1216
|
+
}
|
|
1217
|
+
if ($BiliDir) { $spec.biliDir = [string]$BiliDir }
|
|
1218
|
+
if ($Sessions -and @($Sessions).Count -gt 0) { $spec.sessions = @($Sessions | ForEach-Object { [string]$_ }) }
|
|
1219
|
+
[IO.File]::WriteAllText($specPath, ($spec | ConvertTo-Json -Depth 5 -Compress), (New-Object Text.UTF8Encoding($false)))
|
|
1220
|
+
|
|
1221
|
+
$oldRunAsNode = $env:ELECTRON_RUN_AS_NODE
|
|
1222
|
+
try {
|
|
1223
|
+
if ($runner.Electron) { $env:ELECTRON_RUN_AS_NODE = '1' }
|
|
1224
|
+
else { Remove-Item Env:\ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue }
|
|
1225
|
+
& $runner.Exe $tool 'sync' $specPath $outPath *> $null
|
|
1226
|
+
$code = $LASTEXITCODE
|
|
1227
|
+
if (-not (Test-Path -LiteralPath $outPath)) {
|
|
1228
|
+
$res.Reason = ('压缩状态工具没有产出结果文件(执行器退出码 {0})' -f $code)
|
|
1229
|
+
return $res
|
|
1230
|
+
}
|
|
1231
|
+
$r = ([IO.File]::ReadAllText($outPath, [Text.Encoding]::UTF8)) | ConvertFrom-Json -ErrorAction Stop
|
|
1232
|
+
$res.Ok = [bool]$r.ok
|
|
1233
|
+
$res.NoBili = [bool]$r.noBili
|
|
1234
|
+
if ($r.biliDir) { $res.BiliDir = [string]$r.biliDir }
|
|
1235
|
+
$res.Exported = @($r.exported)
|
|
1236
|
+
$res.Imported = @($r.imported)
|
|
1237
|
+
$res.Missing = @($r.missing)
|
|
1238
|
+
$res.Failed = @($r.failed)
|
|
1239
|
+
foreach ($e in @($r.exported)) { if ($e -and $e.bytes) { $res.Bytes += [long]$e.bytes } }
|
|
1240
|
+
$res.Skipped = $false
|
|
1241
|
+
return $res
|
|
1242
|
+
} catch {
|
|
1243
|
+
$res.Reason = ('压缩状态工具执行出错:{0}' -f $_.Exception.Message)
|
|
1244
|
+
return $res
|
|
1245
|
+
} finally {
|
|
1246
|
+
if ($null -eq $oldRunAsNode) { Remove-Item Env:\ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue }
|
|
1247
|
+
else { $env:ELECTRON_RUN_AS_NODE = $oldRunAsNode }
|
|
1248
|
+
Remove-Item -LiteralPath $specPath -Force -ErrorAction SilentlyContinue
|
|
1249
|
+
Remove-Item -LiteralPath $outPath -Force -ErrorAction SilentlyContinue
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
# 把压缩状态搬运结果变成用户能看懂的提示;导入失败一律进 context-state 通知。
|
|
1254
|
+
function Write-BiliStateSyncReport($State, [string]$DshHome) {
|
|
1255
|
+
if (-not $State) { return }
|
|
1256
|
+
if ($State.Skipped) {
|
|
1257
|
+
Warn ('billion-context 压缩状态本次未搬运({0})' -f $State.Reason)
|
|
1258
|
+
return
|
|
1259
|
+
}
|
|
1260
|
+
if ($State.NoBili) {
|
|
1261
|
+
if ($State.Mode -eq 'import') {
|
|
1262
|
+
Ok ('本机没有 billion-context,跳过压缩状态导入(DSH 自带压缩会接管)')
|
|
1263
|
+
if ($DshHome) {
|
|
1264
|
+
Add-SyncNotice -Kind 'context-state' -Path 'billion-context' -Reason '本机没有 billion-context:会话压缩记录无处导入,DSH 自带压缩会接管;若长会话报上下文超长,在主力机重推一次或在配置里关掉 billion-context。'
|
|
1265
|
+
}
|
|
1266
|
+
} else {
|
|
1267
|
+
Ok ('本机没有 billion-context,跳过压缩状态导出')
|
|
1268
|
+
}
|
|
1269
|
+
return
|
|
1270
|
+
}
|
|
1271
|
+
if ($State.Mode -eq 'export') {
|
|
1272
|
+
$n = @($State.Exported).Count
|
|
1273
|
+
Ok ('billion-context 压缩状态(抓取):导出 {0} 个会话、{1:N1} MB' -f $n, ($State.Bytes / 1MB))
|
|
1274
|
+
foreach ($e in @($State.Exported)) {
|
|
1275
|
+
Ok (' {0}:blocks {1}、refs {2}、summary {3}' -f [string]$e.sessionId, $e.blocks, $e.refs, $e.content)
|
|
1276
|
+
}
|
|
1277
|
+
} else {
|
|
1278
|
+
$n = @($State.Imported).Count
|
|
1279
|
+
Ok ('billion-context 压缩状态(还原):导入 {0} 个会话' -f $n)
|
|
1280
|
+
foreach ($e in @($State.Imported)) {
|
|
1281
|
+
Ok (' {0}:blocks +{1}、refs +{2}、summary +{3}' -f [string]$e.sessionId, $e.blocks, $e.refs, $e.content)
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
foreach ($m in @($State.Missing)) {
|
|
1285
|
+
Warn (' 仓库里没有这个会话的压缩记录:{0}(B 机继续该会话可能报上下文超长)' -f [string]$m)
|
|
1286
|
+
if ($DshHome -and $State.Mode -eq 'import') {
|
|
1287
|
+
Add-SyncNotice -Kind 'context-state' -Path ([string]$m) -Reason '会话压缩状态没同步过来:本机继续该会话可能报上下文超长。在主力机重推一次(抓取会带上压缩状态),或在本机设置 BILLION_CONTEXT_PLUGIN=0 让 DSH 原生压缩兜底。'
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
foreach ($f in @($State.Failed)) {
|
|
1291
|
+
$sid = if ($f.sessionId) { [string]$f.sessionId } elseif ($f.file) { [string]$f.file } else { '(未知)' }
|
|
1292
|
+
Warn (' 压缩状态处理失败:{0}({1})' -f $sid, [string]$f.error)
|
|
1293
|
+
if ($DshHome) { Add-SyncNotice -Kind 'context-state' -Path $sid -Reason ('billion-context 压缩状态搬运失败:' + [string]$f.error) }
|
|
1294
|
+
}
|
|
1295
|
+
}
|
package/runtime/post-exit.ps1
CHANGED
|
@@ -169,6 +169,8 @@ function Show-NoticesSummary {
|
|
|
169
169
|
try { $n = Get-Content -LiteralPath $np -Raw -Encoding utf8 | ConvertFrom-Json } catch { return }
|
|
170
170
|
if (-not $n -or [int]$n.total -le 0) { return }
|
|
171
171
|
$groups = @(
|
|
172
|
+
@{ Kind = 'plugin-missing'; Title = '插件没装在本机(先装插件,再重新拉取一次)' }
|
|
173
|
+
@{ Kind = 'context-state'; Title = '会话缺 billion-context 压缩状态(可能报上下文超长)' }
|
|
172
174
|
@{ Kind = 'history'; Title = '历史提交里的超限文件(远端会拒收,需换仓库或清史)' }
|
|
173
175
|
@{ Kind = 'too-large'; Title = '超过单文件上限,未进仓库' }
|
|
174
176
|
@{ Kind = 'failed'; Title = '写入/推送失败' }
|