pantheon-opencode 1.6.0-beta.10 → 1.6.0-beta.11
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/AGENTS.global.md +42 -18
- package/AGENTS.md +1 -1
- package/CHANGELOG.md +24 -0
- package/README.md +66 -21
- package/bin/pantheon-init.mjs +44 -1
- package/package.json +4 -4
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/build-agents-md.mjs +60 -6
- package/scripts/install/backup.mjs +475 -0
- package/scripts/install/cli-ui.mjs +6 -1
- package/scripts/install/exit-codes.mjs +26 -0
- package/scripts/install/interactive.mjs +15 -1
- package/scripts/install/migrate.mjs +41 -22
- package/scripts/install/opencode.mjs +562 -50
- package/scripts/install/plan.mjs +287 -0
- package/scripts/install/plugin.mjs +22 -8
- package/scripts/install/shared.mjs +89 -55
- package/scripts/install/strings.mjs +20 -0
- package/scripts/install/venv.mjs +13 -3
- package/scripts/install/vision-prompt.mjs +11 -1
- package/scripts/mcp_persistence.py +50 -15
- package/scripts/prune.mjs +9 -3
- package/scripts/restore.mjs +222 -0
- package/skills-lock.json +1 -1
- package/src/agents/iris.md +6 -0
- package/src/agents/zeus.md +19 -62
- package/src/instructions/agent-return-format.instructions.md +17 -15
- package/src/instructions/memory-protocol.instructions.md +3 -2
- package/src/instructions/test-economy.instructions.md +21 -0
- package/src/instructions/zeus-anti-stall.instructions.md +6 -6
- package/src/instructions/zeus-council-synthesis.instructions.md +10 -11
- package/src/instructions/zeus-memory-operations.instructions.md +10 -6
- package/src/instructions/zeus-timeout-retry.instructions.md +28 -9
- package/src/mcp/mcp_persistence.py +50 -15
- package/src/pantheon/cost-command.ts +5 -2
- package/src/pantheon/model-agents.ts +19 -0
- package/src/pantheon/model-command.ts +218 -56
- package/src/pantheon/v2-model-overrides.ts +275 -0
- package/src/pantheon/v2-tools.ts +18 -29
- package/src/plugin-v2.ts +13 -0
- package/src/plugins/tui/dist/tui-v2.js +2 -2
- package/src/plugins/tui/dist/tui.js +1 -1
- package/src/plugins/tui/package-lock.json +2 -2
- package/src/plugins/tui/package.json +1 -1
- package/src/skills/auto-continue/SKILL.md +14 -6
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* backup.mjs — Exact backup sets for the Pantheon installer (Beta 11 Setup,
|
|
3
|
+
* phase 2).
|
|
4
|
+
*
|
|
5
|
+
* Before the installer modifies or creates a file, the CURRENT state of every
|
|
6
|
+
* affected path is preserved byte-for-byte under
|
|
7
|
+
* `<configDir>/.pantheon/backups/<UTC-timestamp>/`, mirroring the path relative
|
|
8
|
+
* to `configDir`, and described by a `manifest.json`. Files that do not exist
|
|
9
|
+
* yet are recorded as absent so a restore can reproduce the original state
|
|
10
|
+
* exactly.
|
|
11
|
+
*
|
|
12
|
+
* SECURITY
|
|
13
|
+
* --------
|
|
14
|
+
* A backup is an exact copy, so it can contain API keys or tokens that were
|
|
15
|
+
* already in the user's files. This module therefore:
|
|
16
|
+
* - never prints file contents, values or hashes (only paths / counts / modes);
|
|
17
|
+
* - writes the backup directory `0700` and every file `0600`.
|
|
18
|
+
* Windows is best-effort: `chmodSync` is attempted and a failure never aborts.
|
|
19
|
+
*
|
|
20
|
+
* SCOPE
|
|
21
|
+
* -----
|
|
22
|
+
* IN scope (byte-for-byte, restorable): every regular file the install can
|
|
23
|
+
* modify, rewrite, or remove INSIDE the config dir — including stale files that
|
|
24
|
+
* a `--clean` run deletes.
|
|
25
|
+
* OUT of scope (recorded in `manifest.notRevertible`, never restored): bulk
|
|
26
|
+
* directories whose byte-for-byte copy is not feasible (`.venv`, the plugin
|
|
27
|
+
* dir with its `node_modules`) and files OUTSIDE the config dir (the global
|
|
28
|
+
* `tui.json` copies and the global `opencode-vision.json`). The manifest states
|
|
29
|
+
* these explicitly rather than silently omitting them.
|
|
30
|
+
*
|
|
31
|
+
* The functions are `pure-ish`: they take explicit paths and an injectable
|
|
32
|
+
* `clock`, return data (never logging), and are safe to unit-test without a
|
|
33
|
+
* running installer.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { createHash } from 'node:crypto'
|
|
37
|
+
import {
|
|
38
|
+
chmodSync,
|
|
39
|
+
existsSync,
|
|
40
|
+
lstatSync,
|
|
41
|
+
mkdirSync,
|
|
42
|
+
readdirSync,
|
|
43
|
+
readFileSync,
|
|
44
|
+
rmSync,
|
|
45
|
+
statSync,
|
|
46
|
+
writeFileSync,
|
|
47
|
+
} from 'node:fs'
|
|
48
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
49
|
+
|
|
50
|
+
/** Directory (relative to the config dir) that holds the backup sets. */
|
|
51
|
+
export const BACKUP_SUBDIR = join('.pantheon', 'backups')
|
|
52
|
+
|
|
53
|
+
/** Number of most-recent backup sets retained. */
|
|
54
|
+
export const DEFAULT_KEEP = 5
|
|
55
|
+
|
|
56
|
+
/** UTC timestamp directory name: `YYYY-MM-DDTHH-MM-SSZ` (filesystem-safe). */
|
|
57
|
+
const TIMESTAMP_RE = /^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z$/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Format a Date as the backup-directory timestamp.
|
|
61
|
+
* @param {Date} date
|
|
62
|
+
* @returns {string}
|
|
63
|
+
*/
|
|
64
|
+
export function formatTimestamp(date = new Date()) {
|
|
65
|
+
return date
|
|
66
|
+
.toISOString()
|
|
67
|
+
.replace(/\.\d+Z$/, 'Z')
|
|
68
|
+
.replace(/:/g, '-')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether a directory name is one of our timestamped backup sets. Used as the
|
|
73
|
+
* guard so retention can never delete a directory we did not create.
|
|
74
|
+
* @param {string} name
|
|
75
|
+
* @returns {boolean}
|
|
76
|
+
*/
|
|
77
|
+
export function isBackupDirName(name) {
|
|
78
|
+
return typeof name === 'string' && TIMESTAMP_RE.test(name)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Absolute path of the backups root for a config dir. */
|
|
82
|
+
export function backupRoot(configDir) {
|
|
83
|
+
return join(configDir, BACKUP_SUBDIR)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Absolute path of a named backup set. */
|
|
87
|
+
export function backupSetPath(configDir, name) {
|
|
88
|
+
return join(backupRoot(configDir), name)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Whether `candidate` is a direct child of `root` (no traversal). Pure string
|
|
93
|
+
* check — the retention path guard.
|
|
94
|
+
* @param {string} root
|
|
95
|
+
* @param {string} candidate
|
|
96
|
+
* @returns {boolean}
|
|
97
|
+
*/
|
|
98
|
+
export function isWithin(root, candidate) {
|
|
99
|
+
const rel = relative(resolve(root), resolve(candidate))
|
|
100
|
+
if (rel === '') return false
|
|
101
|
+
return (
|
|
102
|
+
!rel.startsWith('..') && !rel.split(sep).includes('..') && !resolve(candidate).startsWith('..')
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Guard for a deletable backup set: it must live directly under
|
|
108
|
+
* `<configDir>/.pantheon/backups/` AND its name must match the timestamp
|
|
109
|
+
* pattern. Nothing else may ever be removed by retention.
|
|
110
|
+
* @param {string} configDir
|
|
111
|
+
* @param {string} dir
|
|
112
|
+
* @returns {boolean}
|
|
113
|
+
*/
|
|
114
|
+
export function isDeletableBackupDir(configDir, dir) {
|
|
115
|
+
const root = backupRoot(configDir)
|
|
116
|
+
if (!isWithin(root, dir)) return false
|
|
117
|
+
const rel = relative(resolve(root), resolve(dir))
|
|
118
|
+
return isBackupDirName(rel)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** List backup sets, newest first. */
|
|
122
|
+
export function listBackupSets(configDir) {
|
|
123
|
+
const root = backupRoot(configDir)
|
|
124
|
+
if (!existsSync(root)) return []
|
|
125
|
+
const sets = []
|
|
126
|
+
for (const name of readdirSync(root)) {
|
|
127
|
+
if (!isBackupDirName(name)) continue
|
|
128
|
+
const dir = join(root, name)
|
|
129
|
+
try {
|
|
130
|
+
if (!statSync(dir).isDirectory()) continue
|
|
131
|
+
} catch {
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
sets.push({ name, dir })
|
|
135
|
+
}
|
|
136
|
+
sets.sort((a, b) => (a.name < b.name ? 1 : a.name > b.name ? -1 : 0))
|
|
137
|
+
return sets
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Choose the next free backup-set name for `configDir`. Second-granularity names
|
|
142
|
+
* collide when two sets are created in the same second (apply immediately
|
|
143
|
+
* followed by a pre-restore snapshot); the seconds are bumped until the name is
|
|
144
|
+
* free so a new set never overwrites an existing one. Shared by the apply path
|
|
145
|
+
* and the dry-run preview so both agree.
|
|
146
|
+
*
|
|
147
|
+
* @param {string} configDir
|
|
148
|
+
* @param {Date} [base]
|
|
149
|
+
* @returns {string}
|
|
150
|
+
*/
|
|
151
|
+
export function nextBackupName(configDir, base = new Date()) {
|
|
152
|
+
let name = formatTimestamp(base)
|
|
153
|
+
let bump = 0
|
|
154
|
+
while (existsSync(backupSetPath(configDir, name))) {
|
|
155
|
+
bump += 1
|
|
156
|
+
name = formatTimestamp(new Date(base.getTime() + bump * 1000))
|
|
157
|
+
}
|
|
158
|
+
return name
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** sha256 hex of a buffer. */
|
|
162
|
+
function sha256(buffer) {
|
|
163
|
+
return createHash('sha256').update(buffer).digest('hex')
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function chmodBestEffort(path, mode) {
|
|
167
|
+
try {
|
|
168
|
+
chmodSync(path, mode)
|
|
169
|
+
} catch {
|
|
170
|
+
// Windows / exotic filesystems: best-effort only.
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Create a directory ONLY when it is absent, and never chmod a directory that
|
|
176
|
+
* already exists. Chmodding a pre-existing directory is an irreversible side
|
|
177
|
+
* effect on the user's tree (F1): restoring into a project would otherwise
|
|
178
|
+
* rewrite the project root to 0700. Bulk-directory modes are out of scope for
|
|
179
|
+
* the exact backup (see `notRevertible`).
|
|
180
|
+
*/
|
|
181
|
+
function ensureDir(path, mode) {
|
|
182
|
+
if (existsSync(path)) return
|
|
183
|
+
try {
|
|
184
|
+
mkdirSync(path, { recursive: true, mode })
|
|
185
|
+
chmodBestEffort(path, mode)
|
|
186
|
+
} catch {
|
|
187
|
+
// A concurrent creator won the race; leave it untouched.
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Create an exact backup set for `entries` (paths relative to `configDir`).
|
|
193
|
+
*
|
|
194
|
+
* A path that exists as a regular file is copied byte-for-byte with mode 0600
|
|
195
|
+
* and hashed; anything else (absent, directory, symlink) is recorded as absent.
|
|
196
|
+
* The whole set is written before this function returns, so a caller can run it
|
|
197
|
+
* BEFORE the first modifying write.
|
|
198
|
+
*
|
|
199
|
+
* @param {object} opts
|
|
200
|
+
* @param {string} opts.configDir
|
|
201
|
+
* @param {string[]} opts.entries - relative paths (deduped, order preserved)
|
|
202
|
+
* @param {string[]} [opts.notRevertible] - paths the run may delete but cannot
|
|
203
|
+
* capture byte-for-byte (bulk dirs like `.venv`/plugin `node_modules`, or
|
|
204
|
+
* out-of-target global files). Recorded in the manifest so the guarantee is
|
|
205
|
+
* honest, never restored.
|
|
206
|
+
* @param {string} [opts.packageVersion]
|
|
207
|
+
* @param {() => Date} [opts.clock]
|
|
208
|
+
* @param {number} [opts.keep=DEFAULT_KEEP] - retained sets (0 = no pruning)
|
|
209
|
+
* @returns {{ dir: string, name: string, manifest: object }}
|
|
210
|
+
*/
|
|
211
|
+
export function createBackupSet({
|
|
212
|
+
configDir,
|
|
213
|
+
entries,
|
|
214
|
+
notRevertible = [],
|
|
215
|
+
packageVersion = 'unknown',
|
|
216
|
+
clock = () => new Date(),
|
|
217
|
+
keep = DEFAULT_KEEP,
|
|
218
|
+
} = {}) {
|
|
219
|
+
const base = clock()
|
|
220
|
+
const name = nextBackupName(configDir, base)
|
|
221
|
+
const dir = backupSetPath(configDir, name)
|
|
222
|
+
ensureDir(backupRoot(configDir), 0o700)
|
|
223
|
+
ensureDir(dir, 0o700)
|
|
224
|
+
|
|
225
|
+
const seen = new Set()
|
|
226
|
+
const manifestEntries = []
|
|
227
|
+
for (const relativePath of entries ?? []) {
|
|
228
|
+
const rel = String(relativePath)
|
|
229
|
+
if (rel === '' || seen.has(rel)) continue
|
|
230
|
+
seen.add(rel)
|
|
231
|
+
const source = join(configDir, rel)
|
|
232
|
+
let stat = null
|
|
233
|
+
try {
|
|
234
|
+
stat = lstatSync(source)
|
|
235
|
+
} catch {
|
|
236
|
+
stat = null
|
|
237
|
+
}
|
|
238
|
+
if (stat?.isFile()) {
|
|
239
|
+
const buffer = readFileSync(source)
|
|
240
|
+
const dest = join(dir, rel)
|
|
241
|
+
ensureDir(dirname(dest), 0o700)
|
|
242
|
+
writeFileSync(dest, buffer, { mode: 0o600 })
|
|
243
|
+
chmodBestEffort(dest, 0o600)
|
|
244
|
+
manifestEntries.push({
|
|
245
|
+
relativePath: rel,
|
|
246
|
+
existed: true,
|
|
247
|
+
sha256: sha256(buffer),
|
|
248
|
+
bytes: buffer.length,
|
|
249
|
+
mode: stat.mode & 0o777,
|
|
250
|
+
})
|
|
251
|
+
} else {
|
|
252
|
+
manifestEntries.push({
|
|
253
|
+
relativePath: rel,
|
|
254
|
+
existed: false,
|
|
255
|
+
sha256: null,
|
|
256
|
+
bytes: 0,
|
|
257
|
+
mode: null,
|
|
258
|
+
})
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const manifest = {
|
|
263
|
+
createdAt: base.toISOString(),
|
|
264
|
+
packageVersion,
|
|
265
|
+
configDir,
|
|
266
|
+
notRevertible: [...new Set((notRevertible ?? []).map(String))],
|
|
267
|
+
entries: manifestEntries,
|
|
268
|
+
}
|
|
269
|
+
const manifestPath = join(dir, 'manifest.json')
|
|
270
|
+
writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, { mode: 0o600 })
|
|
271
|
+
chmodBestEffort(manifestPath, 0o600)
|
|
272
|
+
|
|
273
|
+
const pruned = keep > 0 ? pruneBackups(configDir, keep) : { removed: [], failures: [] }
|
|
274
|
+
return { dir, name, manifest, pruned }
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* A manifest entry path must be a plain relative path inside the config dir —
|
|
279
|
+
* no absolute path and no `..` segment (the backup mirrors the config dir).
|
|
280
|
+
* Rejected at read time so a crafted manifest can never steer verify or restore
|
|
281
|
+
* outside the tree.
|
|
282
|
+
* @param {unknown} rel
|
|
283
|
+
* @returns {boolean}
|
|
284
|
+
*/
|
|
285
|
+
function isSafeRelativePath(rel) {
|
|
286
|
+
if (typeof rel !== 'string' || rel === '') return false
|
|
287
|
+
if (isAbsolute(rel)) return false
|
|
288
|
+
return !rel.split(/[\\/]/).includes('..')
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Read and validate the manifest of a backup set.
|
|
293
|
+
*
|
|
294
|
+
* VALIDATION SCOPE (documented, not implied): this rejects a malformed manifest
|
|
295
|
+
* and any entry whose `relativePath` is absolute or contains `..` (fail closed).
|
|
296
|
+
* It does NOT make the manifest self-verifying — a DROPPED entry or a flipped
|
|
297
|
+
* `existed` flag cannot be detected from the manifest alone. Tampering with the
|
|
298
|
+
* CONTENT of a listed file (newline/truncation/hash change) IS detected by
|
|
299
|
+
* {@link verifyBackupSet}, because each listed file is re-hashed.
|
|
300
|
+
*
|
|
301
|
+
* @param {string} dir
|
|
302
|
+
* @returns {{ ok: true, manifest: object } | { ok: false, reason: string }}
|
|
303
|
+
*/
|
|
304
|
+
export function readManifest(dir) {
|
|
305
|
+
const manifestPath = join(dir, 'manifest.json')
|
|
306
|
+
if (!existsSync(manifestPath)) return { ok: false, reason: 'missing-manifest' }
|
|
307
|
+
try {
|
|
308
|
+
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
|
|
309
|
+
if (!manifest || !Array.isArray(manifest.entries)) {
|
|
310
|
+
return { ok: false, reason: 'invalid-manifest' }
|
|
311
|
+
}
|
|
312
|
+
if (manifest.entries.some((e) => !e || !isSafeRelativePath(e.relativePath))) {
|
|
313
|
+
return { ok: false, reason: 'unsafe-path' }
|
|
314
|
+
}
|
|
315
|
+
return { ok: true, manifest }
|
|
316
|
+
} catch {
|
|
317
|
+
return { ok: false, reason: 'invalid-manifest' }
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Verify every backed-up file still matches its manifest hash. Any missing file
|
|
323
|
+
* or hash mismatch is reported (no silent partial restore).
|
|
324
|
+
* @param {string} dir
|
|
325
|
+
* @returns {{ ok: boolean, reason?: string, mismatches: string[] }}
|
|
326
|
+
*/
|
|
327
|
+
export function verifyBackupSet(dir) {
|
|
328
|
+
const read = readManifest(dir)
|
|
329
|
+
if (!read.ok) return { ok: false, reason: read.reason, mismatches: [] }
|
|
330
|
+
const mismatches = []
|
|
331
|
+
for (const entry of read.manifest.entries) {
|
|
332
|
+
if (!entry.existed) continue
|
|
333
|
+
const file = join(dir, entry.relativePath)
|
|
334
|
+
if (!existsSync(file)) {
|
|
335
|
+
mismatches.push(entry.relativePath)
|
|
336
|
+
continue
|
|
337
|
+
}
|
|
338
|
+
if (sha256(readFileSync(file)) !== entry.sha256) mismatches.push(entry.relativePath)
|
|
339
|
+
}
|
|
340
|
+
return { ok: mismatches.length === 0, mismatches }
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Order for writes: `opencode.json` strictly last. */
|
|
344
|
+
function restoreOrder(a, b) {
|
|
345
|
+
const last = (rel) => (rel === 'opencode.json' ? 1 : 0)
|
|
346
|
+
if (last(a.relativePath) !== last(b.relativePath))
|
|
347
|
+
return last(a.relativePath) - last(b.relativePath)
|
|
348
|
+
return a.relativePath.localeCompare(b.relativePath)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Compute the restore plan by PATH ONLY (no contents exposed). Absent-in-backup
|
|
353
|
+
* entries that now exist become `remove`; a present target is hashed internally
|
|
354
|
+
* to classify it as `unchanged` or `update`. Hashes are never returned or
|
|
355
|
+
* printed at this layer — only `{ relativePath, action }`.
|
|
356
|
+
*
|
|
357
|
+
* @param {string} dir - backup set directory
|
|
358
|
+
* @param {string} configDir
|
|
359
|
+
* @returns {{ entries: Array<{relativePath: string, action: string}>, manifest: object } | { error: string }}
|
|
360
|
+
*/
|
|
361
|
+
export function planRestore(dir, configDir) {
|
|
362
|
+
const read = readManifest(dir)
|
|
363
|
+
if (!read.ok) return { error: read.reason }
|
|
364
|
+
const verify = verifyBackupSet(dir)
|
|
365
|
+
if (!verify.ok) return { error: verify.reason ?? 'integrity', mismatches: verify.mismatches }
|
|
366
|
+
|
|
367
|
+
const entries = []
|
|
368
|
+
for (const entry of [...read.manifest.entries].sort(restoreOrder)) {
|
|
369
|
+
const target = join(configDir, entry.relativePath)
|
|
370
|
+
if (entry.existed) {
|
|
371
|
+
let action = 'create'
|
|
372
|
+
try {
|
|
373
|
+
const st = lstatSync(target)
|
|
374
|
+
if (st.isFile()) {
|
|
375
|
+
const current = readFileSync(target)
|
|
376
|
+
action = sha256(current) === entry.sha256 ? 'unchanged' : 'update'
|
|
377
|
+
} else {
|
|
378
|
+
action = 'update'
|
|
379
|
+
}
|
|
380
|
+
} catch {
|
|
381
|
+
action = 'create'
|
|
382
|
+
}
|
|
383
|
+
entries.push({ relativePath: entry.relativePath, action })
|
|
384
|
+
} else {
|
|
385
|
+
const exists = existsSync(target)
|
|
386
|
+
entries.push({ relativePath: entry.relativePath, action: exists ? 'remove' : 'unchanged' })
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return { entries, manifest: read.manifest }
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Restore a verified backup set into `configDir`. Refuses (throws) if the
|
|
394
|
+
* manifest is missing/invalid or any backed-up file is tampered.
|
|
395
|
+
*
|
|
396
|
+
* @param {string} dir
|
|
397
|
+
* @param {string} configDir
|
|
398
|
+
* @param {{ dryRun?: boolean }} [opts]
|
|
399
|
+
* @returns {{ entries: Array<{relativePath: string, action: string}> }}
|
|
400
|
+
*/
|
|
401
|
+
export function restoreBackupSet(dir, configDir, { dryRun = false } = {}) {
|
|
402
|
+
const plan = planRestore(dir, configDir)
|
|
403
|
+
if (plan.error) {
|
|
404
|
+
const detail = plan.mismatches?.length ? ` (${plan.mismatches.length} mismatch(es))` : ''
|
|
405
|
+
throw new Error(`Backup is not restorable: ${plan.error}${detail}`)
|
|
406
|
+
}
|
|
407
|
+
if (dryRun) return { entries: plan.entries }
|
|
408
|
+
|
|
409
|
+
for (const item of plan.entries) {
|
|
410
|
+
const backUpFile = join(dir, item.relativePath)
|
|
411
|
+
const target = join(configDir, item.relativePath)
|
|
412
|
+
if (item.action === 'unchanged') continue
|
|
413
|
+
|
|
414
|
+
// Refuse to write through/over a symlink (mirrors writeIfChanged).
|
|
415
|
+
let linkStat = null
|
|
416
|
+
try {
|
|
417
|
+
linkStat = lstatSync(target)
|
|
418
|
+
} catch {
|
|
419
|
+
linkStat = null
|
|
420
|
+
}
|
|
421
|
+
if (linkStat?.isSymbolicLink()) {
|
|
422
|
+
throw new Error(`Refusing to restore through symlink: ${item.relativePath}`)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (item.action === 'remove') {
|
|
426
|
+
if (linkStat?.isFile()) rmSync(target, { force: true })
|
|
427
|
+
continue
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const buffer = readFileSync(backUpFile)
|
|
431
|
+
ensureDir(dirname(target), 0o700)
|
|
432
|
+
writeFileSync(target, buffer, { mode: 0o600 })
|
|
433
|
+
const recorded = plan.manifest.entries.find((e) => e.relativePath === item.relativePath)
|
|
434
|
+
if (recorded && recorded.mode !== null) chmodBestEffort(target, recorded.mode)
|
|
435
|
+
}
|
|
436
|
+
return { entries: plan.entries }
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Delete all but the `keep` most-recent backup sets under the config dir.
|
|
441
|
+
* Only directories that pass {@link isDeletableBackupDir} are ever removed;
|
|
442
|
+
* deletion failures are reported, never thrown.
|
|
443
|
+
*
|
|
444
|
+
* @param {string} configDir
|
|
445
|
+
* @param {number} [keep=DEFAULT_KEEP]
|
|
446
|
+
* @returns {{ removed: string[], failures: string[] }}
|
|
447
|
+
*/
|
|
448
|
+
export function pruneBackups(configDir, keep = DEFAULT_KEEP) {
|
|
449
|
+
const removed = []
|
|
450
|
+
const failures = []
|
|
451
|
+
const sets = listBackupSets(configDir)
|
|
452
|
+
for (const set of sets.slice(keep)) {
|
|
453
|
+
if (!isDeletableBackupDir(configDir, set.dir)) continue
|
|
454
|
+
try {
|
|
455
|
+
rmSync(set.dir, { recursive: true, force: true })
|
|
456
|
+
removed.push(set.name)
|
|
457
|
+
} catch {
|
|
458
|
+
failures.push(set.name)
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return { removed, failures }
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* The one-time, exact, secret-safe warning printed before the installer applies.
|
|
466
|
+
* @param {string} backupPath
|
|
467
|
+
* @returns {string}
|
|
468
|
+
*/
|
|
469
|
+
export function backupWarning(backupPath) {
|
|
470
|
+
return (
|
|
471
|
+
`Backup created at ${backupPath} (mode 0600). It is an exact copy of your ` +
|
|
472
|
+
`current files and therefore also contains any API keys or tokens already ` +
|
|
473
|
+
`present in them. It is never printed. Restore with: pantheon-opencode restore`
|
|
474
|
+
)
|
|
475
|
+
}
|
|
@@ -33,7 +33,12 @@ export function configure(opts = {}) {
|
|
|
33
33
|
if (opts.dryRun !== undefined) _dryRun = opts.dryRun
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Prefix a message with the `[DRY-RUN]` marker while a dry run is configured.
|
|
38
|
+
* Exported so callers that print through raw `console.log` (e.g. the per-file
|
|
39
|
+
* change plan) stay consistent with every other dry-run line.
|
|
40
|
+
*/
|
|
41
|
+
export function tag(msg) {
|
|
37
42
|
return _dryRun ? `[DRY-RUN] ${msg}` : msg
|
|
38
43
|
}
|
|
39
44
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* exit-codes.mjs — the process exit code the `init` CLI returns for an
|
|
3
|
+
* `installOpenCode` result. Pure, so the mapping is unit-testable without
|
|
4
|
+
* spawning the CLI.
|
|
5
|
+
*
|
|
6
|
+
* `0` success, or plan-only / declined (nothing applied)
|
|
7
|
+
* `1` failure (the CLI handles thrown errors separately)
|
|
8
|
+
* `2` apply requested without authorization (non-interactive, no --yes)
|
|
9
|
+
* `3` drift detected — a planned target changed between plan and apply
|
|
10
|
+
* `130` canceled
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export function resolveInstallExitCode(result, isDryRun) {
|
|
14
|
+
if (isDryRun) return 0
|
|
15
|
+
if (result?.applied !== false) return 0
|
|
16
|
+
switch (result.reason) {
|
|
17
|
+
case 'not-authorized':
|
|
18
|
+
return 2
|
|
19
|
+
case 'drift':
|
|
20
|
+
return 3
|
|
21
|
+
case 'canceled':
|
|
22
|
+
return 130
|
|
23
|
+
default:
|
|
24
|
+
return 0
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -184,7 +184,7 @@ export async function promptConfirm(message, { default: def = true } = {}) {
|
|
|
184
184
|
reject(new Error('Canceled'))
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
function handler(
|
|
187
|
+
function handler(str, key) {
|
|
188
188
|
if (!key) return
|
|
189
189
|
if ((key.ctrl && key.name === 'c') || key.name === 'escape') {
|
|
190
190
|
cancel()
|
|
@@ -208,6 +208,20 @@ export async function promptConfirm(message, { default: def = true } = {}) {
|
|
|
208
208
|
})
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Apply-gate prompt. Unlike the pre-install confirm this defaults to NO, so a
|
|
213
|
+
* bare Enter never authorizes a write. In a non-TTY it returns the default
|
|
214
|
+
* (`false`) without blocking — callers that must not prompt at all should branch
|
|
215
|
+
* on TTY before calling.
|
|
216
|
+
*
|
|
217
|
+
* @param {string} planSummary e.g. "Apply these 12 changes?"
|
|
218
|
+
* @param {{ default?: boolean }} [opts]
|
|
219
|
+
* @returns {Promise<boolean>}
|
|
220
|
+
*/
|
|
221
|
+
export async function promptReview(planSummary, { default: def = false } = {}) {
|
|
222
|
+
return promptConfirm(planSummary, { default: def })
|
|
223
|
+
}
|
|
224
|
+
|
|
211
225
|
export async function promptList(options, { title = '' } = {}) {
|
|
212
226
|
if (options.length === 0) throw new Error('No options provided')
|
|
213
227
|
if (options.length === 1) return options[0].value
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
12
12
|
import { dirname, join } from 'node:path'
|
|
13
13
|
import { fileURLToPath } from 'node:url'
|
|
14
|
+
import { ACTIONS, recordChange, snapshot } from './plan.mjs'
|
|
14
15
|
import { createInitialState, readState, writeState } from './state.mjs'
|
|
15
16
|
|
|
16
17
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
@@ -44,10 +45,10 @@ export function detectVersion(target) {
|
|
|
44
45
|
*
|
|
45
46
|
* @param {string} target - Installation directory
|
|
46
47
|
* @param {string|null} currentVersion - Detected version (null = fresh install)
|
|
47
|
-
* @param {{ dryRun?: boolean }} [options]
|
|
48
|
+
* @param {{ dryRun?: boolean, recorder?: (change: object) => void }} [options]
|
|
48
49
|
* @returns {{ applied: number, messages: string[] }}
|
|
49
50
|
*/
|
|
50
|
-
export function runMigrations(target, currentVersion, { dryRun = false } = {}) {
|
|
51
|
+
export function runMigrations(target, currentVersion, { dryRun = false, recorder = null } = {}) {
|
|
51
52
|
const applied = []
|
|
52
53
|
const semver = parseSemver(currentVersion)
|
|
53
54
|
|
|
@@ -104,28 +105,46 @@ export function runMigrations(target, currentVersion, { dryRun = false } = {}) {
|
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
// ── Write updated state ───────────────────────────────────────────
|
|
107
|
-
if (applied.length > 0
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
108
|
+
if (applied.length > 0) {
|
|
109
|
+
const statePath = join(target, '.pantheon', 'install-state.json')
|
|
110
|
+
if (dryRun) {
|
|
111
|
+
// State-only bookkeeping write: recorded so the preview is complete, but
|
|
112
|
+
// unscored because the install summary does not count it. Classified as
|
|
113
|
+
// an update — a migration that applies implies a state file existed.
|
|
114
|
+
recordChange({ path: statePath, action: ACTIONS.UPDATE, bytes: 0, scored: false }, recorder)
|
|
115
|
+
} else {
|
|
116
|
+
const newVersion = formatSemver(semver)
|
|
117
|
+
let state = readState(target)
|
|
118
|
+
const stateExisted = state !== null
|
|
119
|
+
if (!state) state = createInitialState(newVersion)
|
|
120
|
+
state.pantheon_version = newVersion
|
|
121
|
+
for (const m of applied) {
|
|
122
|
+
state.applied_migrations.push({
|
|
123
|
+
from: m.from,
|
|
124
|
+
to: m.to,
|
|
125
|
+
applied_at: new Date().toISOString(),
|
|
126
|
+
status: 'completed',
|
|
127
|
+
})
|
|
126
128
|
}
|
|
129
|
+
if (!state.components) {
|
|
130
|
+
state.components = {
|
|
131
|
+
agents: { version: 1, status: 'installed' },
|
|
132
|
+
skills: { version: 1, status: 'installed' },
|
|
133
|
+
plugins: { version: 1, status: 'installed' },
|
|
134
|
+
runtime: { version: 1, status: 'installed' },
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
writeState(target, state)
|
|
138
|
+
recordChange(
|
|
139
|
+
{
|
|
140
|
+
path: statePath,
|
|
141
|
+
action: stateExisted ? ACTIONS.UPDATE : ACTIONS.CREATE,
|
|
142
|
+
bytes: snapshot(statePath).size,
|
|
143
|
+
scored: false,
|
|
144
|
+
},
|
|
145
|
+
recorder,
|
|
146
|
+
)
|
|
127
147
|
}
|
|
128
|
-
writeState(target, state)
|
|
129
148
|
}
|
|
130
149
|
|
|
131
150
|
return {
|