@aiwayds/dsh-opentui 0.27.1 → 0.28.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.
- package/CHANGELOG.md +38 -15
- package/README.md +38 -182
- package/bin/dsh-opentui.mjs +54 -258
- package/bin/lib/launcher.mjs +29 -187
- package/dist/client/index.js +7585 -16584
- package/dist/client/index.js.map +26 -51
- package/dist/locales/en.json +21 -8
- package/dist/locales/zh-CN.json +21 -8
- package/package.json +7 -11
- package/scripts/lib/companion-policy.mjs +16 -0
- package/scripts/lib/inproc-companions.mjs +128 -38
- package/scripts/lib/migration-io.mjs +231 -0
- package/scripts/lib/migration.mjs +724 -0
- package/scripts/lib/patch-ownership-state.mjs +42 -0
- package/scripts/lib/patch-ownership.mjs +1428 -0
- package/scripts/lib/profile-lock.mjs +185 -0
- package/scripts/lib/profile-name.d.mts +1 -0
- package/scripts/lib/profile-name.mjs +2 -0
- package/scripts/lib/provision-run-lock.mjs +223 -0
- package/scripts/provision-inproc-profile.mjs +601 -286
- package/dist/host-launch.mjs +0 -1293
- package/dist/host-launch.mjs.map +0 -19
- package/dist/scripts/idle-reaper.mjs +0 -173
- package/dist/watchdog.mjs +0 -476
- package/dist/watchdog.mjs.map +0 -16
- package/scripts/e2e-client.mjs +0 -1122
- package/scripts/e2e-sweep.mjs +0 -932
- package/scripts/idle-reaper.mjs +0 -173
- package/scripts/lib/provision-plan.mjs +0 -899
- package/scripts/lib/provision-state.mjs +0 -294
- package/scripts/lib/upgrade-plan.mjs +0 -489
- package/scripts/provision-opentui-profile.mjs +0 -806
- package/scripts/upgrade.mjs +0 -1149
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* T8b — migration I/O helper (06 §7.4/§7.5; D6 §二/§三).
|
|
3
|
+
*
|
|
4
|
+
* Split from the pure planner (`migration.mjs`): this module is the only place
|
|
5
|
+
* that touches the filesystem. It never reads `~/.dsh` on its own — every path
|
|
6
|
+
* comes from its arguments, so tests can point it at a scratch home.
|
|
7
|
+
*
|
|
8
|
+
* Contract:
|
|
9
|
+
* readMigrationInputs({ dshHome, profileDir, templatePatchText })
|
|
10
|
+
* -> { markerExists, marker, inprocPatchText, opentuiPatchText,
|
|
11
|
+
* opentuiConfigText, opentuiKeybindingsText, opentuiProviderKeysText }
|
|
12
|
+
* applyMigrationPlan({ profileDir, plan, version, now = new Date(), backupsOnly = false, skipBackups = false })
|
|
13
|
+
* -> { wroteConfig, wroteKeybindings, backupsWritten, markerWritten, notices }
|
|
14
|
+
*
|
|
15
|
+
* Hard rules:
|
|
16
|
+
* - readMigrationInputs is read-only and creates no directory;
|
|
17
|
+
* - marker present on entry => the legacy files are NEVER read (an EACCES /
|
|
18
|
+
* EISDIR artifact must not break an already-migrated machine's startup);
|
|
19
|
+
* - invalid config/keybindings payloads are NOT written to the active files;
|
|
20
|
+
* - marker is written LAST; any write failure throws before the marker;
|
|
21
|
+
* - backupsOnly writes ONLY backups (wroteConfig / wroteKeybindings /
|
|
22
|
+
* markerWritten all stay false) — used to snapshot the source before
|
|
23
|
+
* converge rewrites it when source B is the target profile;
|
|
24
|
+
* - skipBackups leaves an existing snapshot untouched on crash re-entry while
|
|
25
|
+
* still writing config / keybindings / marker;
|
|
26
|
+
* - marker present on entry => immediate no-op (no overwrite, no notice replay);
|
|
27
|
+
* - every write is tmp+rename in the target's own directory, mode 0600 for
|
|
28
|
+
* active files / marker / provider-keys backup, 0644 for other backups;
|
|
29
|
+
* - equal content is skipped (no mtime churn); a differing backup rotates a
|
|
30
|
+
* single `.bak` before being overwritten;
|
|
31
|
+
* - no console output — notices are returned for the caller to print. A
|
|
32
|
+
* "已备份" claim is dropped unless a matching backup file exists.
|
|
33
|
+
*/
|
|
34
|
+
import { createHash, randomBytes } from 'node:crypto'
|
|
35
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
36
|
+
import { join } from 'node:path'
|
|
37
|
+
|
|
38
|
+
export const MARKER_NAME = '.migration.json'
|
|
39
|
+
export const BACKUP_DIR = '.migrated-from'
|
|
40
|
+
|
|
41
|
+
function markerPath(profileDir) {
|
|
42
|
+
return join(profileDir, MARKER_NAME)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Create the new profile lazily, only when there is a payload to write. */
|
|
46
|
+
function ensureProfileDir(profileDir) {
|
|
47
|
+
if (!existsSync(profileDir)) mkdirSync(profileDir, { recursive: true, mode: 0o700 })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Text of a file, or undefined when it does not exist. Other IO errors throw. */
|
|
51
|
+
function readTextIfExists(path) {
|
|
52
|
+
try {
|
|
53
|
+
return readFileSync(path, 'utf8')
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (error && error.code === 'ENOENT') return undefined
|
|
56
|
+
throw error
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** tmp + rename inside the target's directory; cleanup on any failure. */
|
|
61
|
+
function atomicWrite(target, content, mode) {
|
|
62
|
+
const tmp = `${target}.tmp-${process.pid}-${randomBytes(8).toString('hex')}`
|
|
63
|
+
try {
|
|
64
|
+
writeFileSync(tmp, content, { mode })
|
|
65
|
+
renameSync(tmp, target)
|
|
66
|
+
} catch (error) {
|
|
67
|
+
try { rmSync(tmp, { force: true }) } catch { /* best effort */ }
|
|
68
|
+
throw error
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Deterministic JSON with sorted object keys (used only for the marker digest). */
|
|
73
|
+
function canonical(value) {
|
|
74
|
+
if (value === undefined || value === null) return 'null'
|
|
75
|
+
if (typeof value !== 'object') return JSON.stringify(value)
|
|
76
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`
|
|
77
|
+
const keys = Object.keys(value).sort()
|
|
78
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(',')}}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** sha256 over { marker, backups } — at/version are deliberately excluded. */
|
|
82
|
+
function markerDigest(marker, backups) {
|
|
83
|
+
const payload = {
|
|
84
|
+
marker,
|
|
85
|
+
backups: backups.map(({ source, kind, text }) => ({ source, kind, text })),
|
|
86
|
+
}
|
|
87
|
+
return createHash('sha256').update(canonical(payload)).digest('hex')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A printed notice must match what is actually on disk. The planner emits an
|
|
92
|
+
* "已备份" claim in its invalid config/keybindings notices, but those payloads
|
|
93
|
+
* are never part of `plan.backups` (only patches and provider-keys are) and no
|
|
94
|
+
* backup file is written for them. Drop the false claim in that case; genuine
|
|
95
|
+
* patch / provider-keys backup notices keep their exact wording.
|
|
96
|
+
*/
|
|
97
|
+
function honestBackupNotices(notices, plan, profileDir) {
|
|
98
|
+
const backedKinds = new Set(
|
|
99
|
+
(Array.isArray(plan.backups) ? plan.backups : []).map((entry) => entry && entry.kind),
|
|
100
|
+
)
|
|
101
|
+
return notices.map((notice) => {
|
|
102
|
+
if (typeof notice !== 'string') return notice
|
|
103
|
+
let text = notice
|
|
104
|
+
for (const name of ['config.json', 'keybindings.json']) {
|
|
105
|
+
if (backedKinds.has(name)) continue
|
|
106
|
+
if (typeof profileDir === 'string' && existsSync(join(profileDir, BACKUP_DIR, `opentui-${name}`))) continue
|
|
107
|
+
text = text.replace(`${name} 解析失败:已备份、`, `${name} 解析失败:`)
|
|
108
|
+
}
|
|
109
|
+
return text
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Read everything the planner needs. No directory creation, no writes.
|
|
115
|
+
* A missing file yields `undefined` for that field; a corrupt marker still sets
|
|
116
|
+
* `markerExists: true` but yields `marker: undefined` (safe direction: block).
|
|
117
|
+
*/
|
|
118
|
+
export function readMigrationInputs({ dshHome, profileDir, templatePatchText } = {}) {
|
|
119
|
+
const mp = markerPath(profileDir)
|
|
120
|
+
const markerExists = existsSync(mp)
|
|
121
|
+
let marker
|
|
122
|
+
if (markerExists) {
|
|
123
|
+
// Migration is already done: do NOT open ANY legacy file. A source that
|
|
124
|
+
// turned into a directory (EISDIR), lost its read bit (EACCES) or holds a
|
|
125
|
+
// half-written payload must never make every later start fail. A corrupt
|
|
126
|
+
// marker takes the same safe route (marker: undefined => planner no-op).
|
|
127
|
+
try {
|
|
128
|
+
marker = JSON.parse(readFileSync(mp, 'utf8'))
|
|
129
|
+
} catch {
|
|
130
|
+
marker = undefined
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
markerExists: true,
|
|
134
|
+
marker,
|
|
135
|
+
inprocPatchText: undefined,
|
|
136
|
+
opentuiPatchText: undefined,
|
|
137
|
+
opentuiConfigText: undefined,
|
|
138
|
+
opentuiKeybindingsText: undefined,
|
|
139
|
+
opentuiProviderKeysText: undefined,
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const legacy = (name) => join(dshHome, 'profiles', name)
|
|
143
|
+
const opentui = legacy('opentui')
|
|
144
|
+
const inproc = legacy('opentui-inproc')
|
|
145
|
+
return {
|
|
146
|
+
markerExists,
|
|
147
|
+
marker,
|
|
148
|
+
inprocPatchText: readTextIfExists(join(inproc, 'cordis.patch.yml')),
|
|
149
|
+
opentuiPatchText: readTextIfExists(join(opentui, 'cordis.patch.yml')),
|
|
150
|
+
opentuiConfigText: readTextIfExists(join(opentui, 'config.json')),
|
|
151
|
+
opentuiKeybindingsText: readTextIfExists(join(opentui, 'keybindings.json')),
|
|
152
|
+
opentuiProviderKeysText: readTextIfExists(join(opentui, 'provider-keys.json')),
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Apply a plan from `planMigration`. Marker is the last write, so a failure in
|
|
158
|
+
* config / keybindings / backups leaves no marker and the next call re-runs.
|
|
159
|
+
* Files already holding the exact payload are skipped (idempotent re-entry).
|
|
160
|
+
*/
|
|
161
|
+
export function applyMigrationPlan({ profileDir, plan = {}, version, now = new Date(), backupsOnly = false, skipBackups = false } = {}) {
|
|
162
|
+
const notices = honestBackupNotices(Array.isArray(plan.notices) ? plan.notices : [], plan, profileDir)
|
|
163
|
+
const result = {
|
|
164
|
+
wroteConfig: false,
|
|
165
|
+
wroteKeybindings: false,
|
|
166
|
+
backupsWritten: [],
|
|
167
|
+
markerWritten: false,
|
|
168
|
+
notices,
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Marker gate: already migrated => no writes and no notice replay.
|
|
172
|
+
if (existsSync(markerPath(profileDir))) {
|
|
173
|
+
return { ...result, notices: [] }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ---- active config / keybindings (invalid payloads are never activated) --
|
|
177
|
+
// backupsOnly snapshots the source BEFORE converge rewrites it; it must not
|
|
178
|
+
// activate any payload, so these flags stay false on that path.
|
|
179
|
+
if (!backupsOnly) {
|
|
180
|
+
const active = [
|
|
181
|
+
['wroteConfig', plan.config, 'config.json'],
|
|
182
|
+
['wroteKeybindings', plan.keybindings, 'keybindings.json'],
|
|
183
|
+
]
|
|
184
|
+
if (active.some(([, payload]) => payload && payload.invalid !== true && typeof payload.text === 'string')) {
|
|
185
|
+
ensureProfileDir(profileDir)
|
|
186
|
+
}
|
|
187
|
+
for (const [flag, payload, name] of active) {
|
|
188
|
+
if (!payload || payload.invalid === true || typeof payload.text !== 'string') continue
|
|
189
|
+
const target = join(profileDir, name)
|
|
190
|
+
if (readTextIfExists(target) === payload.text) continue
|
|
191
|
+
atomicWrite(target, payload.text, 0o600)
|
|
192
|
+
result[flag] = true
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ---- backups (copy into the new profile; old profiles stay untouched) ----
|
|
197
|
+
const backups = Array.isArray(plan.backups) ? plan.backups : []
|
|
198
|
+
if (backups.length > 0 && !skipBackups) {
|
|
199
|
+
const dir = join(profileDir, BACKUP_DIR)
|
|
200
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
201
|
+
for (const entry of backups) {
|
|
202
|
+
if (!entry || !entry.source || !entry.kind || typeof entry.text !== 'string') continue
|
|
203
|
+
const mode = entry.kind === 'provider-keys.json' ? 0o600 : 0o644
|
|
204
|
+
const dest = join(dir, `${entry.source}-${entry.kind}`)
|
|
205
|
+
const existing = readTextIfExists(dest)
|
|
206
|
+
if (existing === entry.text) continue
|
|
207
|
+
if (existing !== undefined) atomicWrite(`${dest}.bak`, existing, mode)
|
|
208
|
+
atomicWrite(dest, entry.text, mode)
|
|
209
|
+
result.backupsWritten.push(`${BACKUP_DIR}/${entry.source}-${entry.kind}`)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ---- marker: last write, and only when the planner allows it -------------
|
|
214
|
+
if (!backupsOnly && plan.marker && plan.canWriteMarker === true) {
|
|
215
|
+
ensureProfileDir(profileDir)
|
|
216
|
+
const record = {
|
|
217
|
+
from: plan.marker.from,
|
|
218
|
+
at: now.toISOString(),
|
|
219
|
+
version,
|
|
220
|
+
digest: markerDigest(plan.marker, backups),
|
|
221
|
+
migrated: plan.marker.migrated,
|
|
222
|
+
dropped: plan.marker.dropped,
|
|
223
|
+
}
|
|
224
|
+
atomicWrite(markerPath(profileDir), `${JSON.stringify(record, null, 2)}\n`, 0o600)
|
|
225
|
+
result.markerWritten = true
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return result
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export default { readMigrationInputs, applyMigrationPlan, MARKER_NAME, BACKUP_DIR }
|