@jamiefutch/pi-backup 1.0.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.
@@ -0,0 +1,94 @@
1
+ #!/bin/bash
2
+ # Pi Clean Reinstall Script
3
+ # Nuclear option - removes ALL user config, skills, extensions, and state,
4
+ # then reinstalls Pi and creates a fresh ~/.pi structure.
5
+ # Run backup-pi.sh FIRST if you want to restore afterward.
6
+
7
+ set -euo pipefail
8
+
9
+ # ─── Config ──────────────────────────────────────────────────────
10
+ PI_ROOT="${PI_ROOT:-$HOME/.pi}"
11
+ PACKAGE="@earendil-works/pi-coding-agent"
12
+ PKG_MANAGER="${PKG_MANAGER:-bun}"
13
+
14
+ # ─── Colors ──────────────────────────────────────────────────────
15
+ RED='\033[0;31m'
16
+ GREEN='\033[0;32m'
17
+ YELLOW='\033[1;33m'
18
+ BLUE='\033[0;34m'
19
+ NC='\033[0m'
20
+
21
+ log() { echo -e "${BLUE}[INFO]${NC} $*"; }
22
+ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
23
+ warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
24
+ err() { echo -e "${RED}[ERR]${NC} $*"; }
25
+
26
+ # ─── Preflight ───────────────────────────────────────────────────
27
+ preflight() {
28
+ echo
29
+ log "════════════════════════════════════════════════════════════"
30
+ log " Pi Clean Reinstall (NUCLEAR OPTION)"
31
+ log "════════════════════════════════════════════════════════════"
32
+ log "Target: $PI_ROOT"
33
+ log "Package: $PACKAGE"
34
+ log "Manager: $PKG_MANAGER"
35
+ echo
36
+
37
+ if [[ -z "${BUN_HOME:-}" ]] && [[ "$PKG_MANAGER" == "bun" ]]; then
38
+ BUN_HOME="$HOME/.bun"
39
+ fi
40
+
41
+ if [[ ! -d "$PI_ROOT" ]]; then
42
+ warn "$PI_ROOT does not exist — skipping removal."
43
+ fi
44
+
45
+ echo
46
+ warn "This will DELETE:"
47
+ warn " $PI_ROOT (ALL config, skills, extensions, memory, sessions)"
48
+ warn " global install of $PACKAGE"
49
+ echo
50
+ warn "Type 'yes' to continue: "
51
+ read -r CONFIRM
52
+ if [[ "$CONFIRM" != "yes" ]]; then
53
+ err "Aborted."
54
+ exit 1
55
+ fi
56
+ echo
57
+ }
58
+
59
+ # ─── Main ────────────────────────────────────────────────────────
60
+ main() {
61
+ preflight
62
+
63
+ # ── 1. Remove everything ─────────────────────────────────────
64
+ log "1/4 Removing $PI_ROOT ..."
65
+ rm -rf "$PI_ROOT"
66
+ ok " $PI_ROOT removed"
67
+
68
+ # ── 2. Uninstall Pi ──────────────────────────────────────────
69
+ log "2/4 Uninstalling $PACKAGE via $PKG_MANAGER ..."
70
+ "$PKG_MANAGER" remove -g "$PACKAGE" || warn " uninstall reported an issue (may already be removed)"
71
+ ok " uninstalled"
72
+
73
+ # ── 3. Reinstall Pi ──────────────────────────────────────────
74
+ log "3/4 Installing $PACKAGE via $PKG_MANAGER ..."
75
+ "$PKG_MANAGER" add -g "$PACKAGE"
76
+ ok " installed"
77
+
78
+ # ── 4. Create fresh structure ────────────────────────────────
79
+ log "4/4 Creating fresh $PI_ROOT structure ..."
80
+ pi --version
81
+ ok " fresh ~/.pi structure created"
82
+
83
+ echo
84
+ log "════════════════════════════════════════════════════════════"
85
+ ok "Clean reinstall complete!"
86
+ echo
87
+ log "Next steps:"
88
+ log " 1. Restore: ~/projects/personal/pi-backup/restore-pi.sh <backup-dir>"
89
+ log " 2. Reinstall npm deps: cd ~/.pi/agent && npm install"
90
+ log " 3. Verify: pi --version, /ctx-doctor, /loop-police-status, /scoped-memory-status"
91
+ echo
92
+ }
93
+
94
+ main "$@"
package/restore-pi.ps1 ADDED
@@ -0,0 +1,255 @@
1
+ # =============================================================
2
+ # Pi Complete Restore Script (Windows PowerShell)
3
+ # Restores a backup created by backup-pi.ps1 after a fresh Pi install
4
+ #
5
+ # Usage:
6
+ # .\restore-pi.ps1 <backup-directory-or-archive>
7
+ # .\restore-pi.ps1 ~\projects\personal\pi-utils\backups\pi-backup-20260811-143000
8
+ # .\restore-pi.ps1 ~\projects\personal\pi-utils\backups\pi-backup-20260811-143000.zip
9
+ # =============================================================
10
+ [CmdletBinding()]
11
+ param(
12
+ [Parameter(Position=0, Mandatory=$false)]
13
+ [string]$BackupArg
14
+ )
15
+
16
+ $ErrorActionPreference = 'Stop'
17
+
18
+ # ─── Config ────────────────────────────────────────────────
19
+ $PiRoot = if ($env:PI_ROOT) { $env:PI_ROOT } else { Join-Path $HOME '.pi' }
20
+
21
+ function Log { Write-Host "[INFO] $args" -ForegroundColor Cyan }
22
+ function OK { Write-Host "[OK] $args" -ForegroundColor Green }
23
+ function Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
24
+ function Err { Write-Host "[ERR] $args" -ForegroundColor Red }
25
+
26
+ function Show-Usage {
27
+ Write-Host @"
28
+ Usage: $($MyInvocation.MyCommand.Name) <backup-directory-or-archive>
29
+
30
+ Restores a Pi backup created by backup-pi.ps1
31
+
32
+ Examples:
33
+ .\$($MyInvocation.MyCommand.Name) ~\projects\personal\pi-utils\backups\pi-backup-20260811-143000
34
+ .\$($MyInvocation.MyCommand.Name) ~\projects\personal\pi-utils\backups\pi-backup-20260811-143000.zip
35
+
36
+ Accepts either an uncompressed backup directory or a compressed zip archive
37
+ (as produced by backup-pi.ps1).
38
+
39
+ Prerequisites:
40
+ - Fresh Pi install completed (pi --version works)
41
+ - Pi NOT running
42
+ "@
43
+ exit 1
44
+ }
45
+
46
+ if (-not $BackupArg) { Show-Usage }
47
+
48
+ # ─── Archive handling ─────────────────────────────────────
49
+ $STAGE = $null
50
+ $BackupDir = $null
51
+
52
+ if (Test-Path -LiteralPath $BackupArg -PathType Container) {
53
+ # Uncompressed directory backup
54
+ $BackupDir = $BackupArg
55
+ } elseif (Test-Path -LiteralPath $BackupArg) {
56
+ # Compressed zip archive: extract to a temp dir first
57
+ $STAGE = Join-Path $env:TEMP ("pi-restore-" + [guid]::NewGuid().ToString('N'))
58
+ New-Item -ItemType Directory -Path $STAGE -Force | Out-Null
59
+ Log "Extracting archive: $BackupArg"
60
+ try {
61
+ Expand-Archive -LiteralPath $BackupArg -DestinationPath $STAGE -Force
62
+ } catch {
63
+ Err "Failed to extract archive: $($_.Exception.Message)"
64
+ Remove-Item -LiteralPath $STAGE -Recurse -Force -ErrorAction SilentlyContinue
65
+ exit 1
66
+ }
67
+ # The archive holds one top-level pi-backup-* dir
68
+ $top = Get-ChildItem -LiteralPath $STAGE -Directory | Select-Object -First 1
69
+ if (-not $top) { Err "No backup directory found inside archive."; Remove-Item $STAGE -Recurse -Force; exit 1 }
70
+ $BackupDir = $top.FullName
71
+ Log "Extracted to: $BackupDir"
72
+ } else {
73
+ Err "Backup not found: $BackupArg (need a directory or .zip)"
74
+ exit 1
75
+ }
76
+
77
+ if (-not (Test-Path -LiteralPath $BackupDir -PathType Container)) {
78
+ Err "Backup directory not found: $BackupDir"
79
+ exit 1
80
+ }
81
+
82
+ # ─── Confirm ──────────────────────────────────────────────
83
+ Write-Host ""
84
+ Warn "================================================================"
85
+ Warn " Pi Complete Restore"
86
+ Warn "================================================================"
87
+ Warn "This will OVERWRITE all current Pi configuration."
88
+ Warn "Backup source: $BackupDir"
89
+ Warn "Target: $PiRoot"
90
+ Warn "Pi must NOT be running."
91
+ Write-Host ""
92
+ $confirm = Read-Host "Continue? [y/N] "
93
+ if ($confirm -notmatch '^[Yy]$') { Log "Aborted."; if ($STAGE) { Remove-Item $STAGE -Recurse -Force }; exit 0 }
94
+
95
+ # ─── Helpers ──────────────────────────────────────────────
96
+ function Restore-File([string]$src, [string]$dst) {
97
+ if (Test-Path -LiteralPath $src -PathType Leaf) {
98
+ $dstDir = Split-Path $dst -Parent
99
+ if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
100
+ Copy-Item -LiteralPath $src -Destination $dst -Force
101
+ OK " $([System.IO.Path]::GetFileName($src))"
102
+ } else {
103
+ Warn " $([System.IO.Path]::GetFileName($src)) - not in backup, skipping"
104
+ }
105
+ }
106
+
107
+ function Restore-Dir([string]$src, [string]$dst) {
108
+ if (Test-Path -LiteralPath $src -PathType Container) {
109
+ $dstDir = Split-Path $dst -Parent
110
+ if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
111
+ if (Test-Path -LiteralPath $dst) { Remove-Item -LiteralPath $dst -Recurse -Force }
112
+ Copy-Item -LiteralPath $src -Destination $dst -Recurse -Force
113
+ OK " $([System.IO.Path]::GetFileName($src))/"
114
+ } else {
115
+ Warn " $([System.IO.Path]::GetFileName($src))/ - not in backup, skipping"
116
+ }
117
+ }
118
+
119
+ function Copy-Tree([string]$src, [string]$dst, [string[]]$excludeDirs = @(), [string[]]$excludeFiles = @()) {
120
+ if (!(Test-Path -LiteralPath $dst)) { New-Item -ItemType Directory -Path $dst -Force | Out-Null }
121
+ if (Get-Command robocopy -ErrorAction SilentlyContinue) {
122
+ $robocopyArgs = @($src, $dst, '/E')
123
+ foreach ($d in $excludeDirs) { $robocopyArgs += @('/XD', $d) }
124
+ foreach ($f in $excludeFiles) { $robocopyArgs += @('/XF', $f) }
125
+ $robocopyArgs += @('/NFL','/NDL','/NJH','/NJS')
126
+ & robocopy @robocopyArgs | Out-Null
127
+ } else {
128
+ Get-ChildItem -LiteralPath $src | ForEach-Object {
129
+ $skip = $false
130
+ if ($_.PSIsContainer -and $_.Name -in $excludeDirs) { $skip = $true }
131
+ if (-not $_.PSIsContainer -and $_.Name -like $excludeFiles) { $skip = $true }
132
+ if (-not $skip) {
133
+ Copy-Item -LiteralPath $_.FullName -Destination (Join-Path $dst $_.Name) -Recurse -Force
134
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ # ─── Main ─────────────────────────────────────────────────
140
+ Write-Host ""
141
+ Log "================================================================"
142
+ Log " Restoring Pi from backup"
143
+ Log "================================================================"
144
+ Write-Host ""
145
+
146
+ # ── 1. Core Config Files ──────────────────────────────────
147
+ Log "1/8 Core configuration files..."
148
+ $agentDir = Join-Path $PiRoot 'agent'
149
+ Restore-File (Join-Path $BackupDir 'settings.json') (Join-Path $agentDir 'settings.json')
150
+ Restore-File (Join-Path $BackupDir 'auth.json') (Join-Path $agentDir 'auth.json')
151
+ Restore-File (Join-Path $BackupDir 'models.json') (Join-Path $agentDir 'models.json')
152
+ Restore-File (Join-Path $BackupDir 'trust.json') (Join-Path $agentDir 'trust.json')
153
+ Restore-File (Join-Path $BackupDir 'keybindings.json') (Join-Path $agentDir 'keybindings.json')
154
+ Restore-File (Join-Path $BackupDir 'pix.json') (Join-Path $agentDir 'pix.json')
155
+ Restore-File (Join-Path $BackupDir 'optimizer.json') (Join-Path $agentDir 'optimizer.json')
156
+ Restore-File (Join-Path $BackupDir 'lmstudio.json.bak') (Join-Path $agentDir 'lmstudio.json.bak')
157
+ Restore-File (Join-Path $BackupDir 'hermes-memory-config.json') (Join-Path $agentDir 'hermes-memory-config.json')
158
+ Restore-File (Join-Path $BackupDir 'pi.code-workspace') (Join-Path $agentDir 'pi.code-workspace')
159
+
160
+ # ── 2. Extension Configs ──────────────────────────────────
161
+ Log "2/8 Extension configurations..."
162
+ Restore-File (Join-Path $BackupDir 'pi-hermes-memory-cleanup\scoped-memory.json') (Join-Path $agentDir 'pi-hermes-memory-cleanup\scoped-memory.json')
163
+ Restore-File (Join-Path $BackupDir 'loop-police.json') (Join-Path $agentDir 'loop-police.json')
164
+ Restore-File (Join-Path $BackupDir 'pi-rtk\config.json') (Join-Path $agentDir 'pi-rtk\config.json')
165
+ Restore-File (Join-Path $BackupDir 'context-mode\config.json') (Join-Path $agentDir 'context-mode\config.json')
166
+ Restore-File (Join-Path $BackupDir 'pi-timeout\config.json') (Join-Path $agentDir 'pi-timeout\config.json')
167
+ Restore-File (Join-Path $BackupDir 'pi-context-viewer\config.json') (Join-Path $agentDir 'pi-context-viewer\config.json')
168
+ Restore-File (Join-Path $BackupDir 'pix-optimizer\config.json') (Join-Path $agentDir 'pix-optimizer\config.json')
169
+ Restore-File (Join-Path $BackupDir 'pi-nvidia-nim\config.json') (Join-Path $agentDir 'pi-nvidia-nim\config.json')
170
+
171
+ # ── 3. Global Skills ──────────────────────────────────────
172
+ Log "3/8 Global skills..."
173
+ Restore-Dir (Join-Path $BackupDir 'skills') (Join-Path $agentDir 'skills')
174
+
175
+ # ── 4. Project Skills & Memory ────────────────────────────
176
+ Log "4/8 Project skills & memory..."
177
+ $projSrc = Join-Path $BackupDir 'projects-memory'
178
+ if (Test-Path -LiteralPath $projSrc -PathType Container) {
179
+ Restore-Dir $projSrc (Join-Path $agentDir 'projects-memory')
180
+ OK " projects-memory/ restored"
181
+ } else {
182
+ Warn " No project memory in backup"
183
+ }
184
+
185
+ # ── 5. Local Extensions ───────────────────────────────────
186
+ Log "5/8 Local extension source code..."
187
+ $localSrc = Join-Path $BackupDir 'local-extensions'
188
+ if (Test-Path -LiteralPath $localSrc -PathType Container) {
189
+ foreach ($ext in Get-ChildItem -LiteralPath $localSrc -Directory) {
190
+ $dst = Join-Path $HOME "projects\personal\$($ext.Name)"
191
+ New-Item -ItemType Directory -Path $dst -Force | Out-Null
192
+ Copy-Tree $ext.FullName $dst
193
+ OK " $($ext.Name)/ restored"
194
+ }
195
+ } else {
196
+ Warn " No local extensions in backup"
197
+ }
198
+
199
+ # ── 6. npm Extension Manifests ────────────────────────────
200
+ Log "6/8 npm extension manifests..."
201
+ $npmSrc = Join-Path $BackupDir 'npm-manifests'
202
+ if (Test-Path -LiteralPath $npmSrc -PathType Container) {
203
+ $npmDst = Join-Path $agentDir 'npm'
204
+ New-Item -ItemType Directory -Path $npmDst -Force | Out-Null
205
+ Copy-Tree $npmSrc $npmDst
206
+ OK " npm manifests restored (run 'npm install' to reinstall deps)"
207
+ } else {
208
+ Warn " No npm manifests in backup"
209
+ }
210
+
211
+ # ── 7. Hermes Memory ──────────────────────────────────────
212
+ Log "7/8 Hermes memory (active files)..."
213
+ $hermesSrc = Join-Path $BackupDir 'pi-hermes-memory'
214
+ if (Test-Path -LiteralPath $hermesSrc -PathType Container) {
215
+ $hermesDst = Join-Path $agentDir 'pi-hermes-memory'
216
+ New-Item -ItemType Directory -Path $hermesDst -Force | Out-Null
217
+ foreach ($f in @('MEMORY.md','USER.md','failures.md','retired-failures.md','retired-memory.md')) {
218
+ Restore-File (Join-Path $hermesSrc $f) (Join-Path $hermesDst $f)
219
+ }
220
+ } else {
221
+ Warn " No Hermes memory in backup"
222
+ }
223
+
224
+ # ── 8. Hermes-cleanup Backups ─────────────────────────────
225
+ Log "8/8 Hermes-cleanup backups..."
226
+ $cleanupSrc = Join-Path $BackupDir 'hermes-cleanup'
227
+ if (Test-Path -LiteralPath $cleanupSrc -PathType Container) {
228
+ foreach ($d in Get-ChildItem -LiteralPath $cleanupSrc -Directory) {
229
+ Restore-Dir $d.FullName (Join-Path $agentDir "hermes-cleanup\$($d.Name)")
230
+ }
231
+ } else {
232
+ Warn " No cleanup backups in backup"
233
+ }
234
+
235
+ # ── Verify ────────────────────────────────────────────────
236
+ Write-Host ""
237
+ Log "================================================================"
238
+ OK "Restore complete!"
239
+ Write-Host ""
240
+ Log "Next steps:"
241
+ Log " 1. Reinstall npm extension dependencies:"
242
+ Log " cd ~/.pi/agent && npm install"
243
+ Log " 2. Verify config:"
244
+ Log " pi --version"
245
+ Log " /ctx-doctor"
246
+ Log " /loop-police-status"
247
+ Log " /scoped-memory-status"
248
+ Log " 3. Restart Pi and verify everything works"
249
+ Write-Host ""
250
+
251
+ # ─── Cleanup ──────────────────────────────────────────────
252
+ if ($STAGE -and (Test-Path -LiteralPath $STAGE)) {
253
+ Remove-Item -LiteralPath $STAGE -Recurse -Force
254
+ Log "Removed temp extraction dir."
255
+ }
package/restore-pi.sh ADDED
@@ -0,0 +1,245 @@
1
+ #!/bin/bash
2
+ # Pi Complete Restore Script
3
+ # Restores a backup created by backup-pi.sh after a fresh Pi install
4
+
5
+ set -euo pipefail
6
+
7
+ # ─── Config ──────────────────────────────────────────────────────
8
+ PI_ROOT="${PI_ROOT:-$HOME/.pi}"
9
+
10
+ # ─── Colors ──────────────────────────────────────────────────────
11
+ RED='\033[0;31m'
12
+ GREEN='\033[0;32m'
13
+ YELLOW='\033[1;33m'
14
+ BLUE='\033[0;34m'
15
+ NC='\033[0m'
16
+
17
+ log() { echo -e "${BLUE}[INFO]${NC} $*"; }
18
+ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
19
+ warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
20
+ err() { echo -e "${RED}[ERR]${NC} $*"; }
21
+
22
+ # ─── Usage ───────────────────────────────────────────────────────
23
+ usage() {
24
+ cat <<EOF
25
+ Usage: $0 <backup-directory-or-archive>
26
+
27
+ Restores a Pi backup created by backup-pi.sh
28
+
29
+ Examples:
30
+ $0 ~/projects/personal/pi-utils/backups/pi-backup-20260811-143000
31
+ $0 ~/projects/personal/pi-utils/backups/pi-backup-20260811-143000.7z
32
+ $0 ~/projects/personal/pi-utils/backups/pi-backup-20260811-143000.zip
33
+
34
+ Accepts either an uncompressed backup directory or a compressed
35
+ zip/7z archive (as produced by backup-pi.sh).
36
+
37
+ Prerequisites:
38
+ - Fresh Pi install completed (pi --version works)
39
+ - Pi NOT running
40
+ EOF
41
+ exit 1
42
+ }
43
+
44
+ # ─── Archive handling ───────────────────────────────────────────
45
+ # Pick a 7zip extraction tool if present, else unzip for .zip.
46
+ pick_extract_tool() {
47
+ if command -v 7zz >/dev/null 2>&1; then echo 7zz
48
+ elif command -v 7z >/dev/null 2>&1; then echo 7z
49
+ elif command -v 7za >/dev/null 2>&1; then echo 7za
50
+ else echo ""; fi
51
+ }
52
+
53
+ [[ $# -eq 1 ]] || usage
54
+ BACKUP_ARG="$1"
55
+ if [[ -d "$BACKUP_ARG" ]]; then
56
+ # Uncompressed directory backup
57
+ BACKUP_DIR="$BACKUP_ARG"
58
+ STAGE=""
59
+ elif [[ -f "$BACKUP_ARG" && "$BACKUP_ARG" =~ \.(zip|7z)$ ]]; then
60
+ # Compressed archive: extract to a temp dir first
61
+ STAGE="$(mktemp -d)"
62
+ log "Extracting archive: $BACKUP_ARG"
63
+ if [[ "$BACKUP_ARG" == *.zip ]]; then
64
+ if command -v unzip >/dev/null 2>&1; then
65
+ unzip -q "$BACKUP_ARG" -d "$STAGE"
66
+ else
67
+ # 7z can also extract zip; fall back to it
68
+ local tool="$(pick_extract_tool)"
69
+ [[ -n "$tool" ]] || { err "No unzip or 7zip available to extract the archive."; exit 1; }
70
+ "$tool" x -o"$STAGE" "$BACKUP_ARG" >/dev/null
71
+ fi
72
+ else
73
+ local tool="$(pick_extract_tool)"
74
+ [[ -n "$tool" ]] || { err "7zip is required to extract .7z backups. Install it."; exit 1; }
75
+ "$tool" x -o"$STAGE" "$BACKUP_ARG" >/dev/null
76
+ fi
77
+ # The archive holds one top-level pi-backup-* dir
78
+ BACKUP_DIR="$(find "$STAGE" -mindepth 1 -maxdepth 1 -type d | head -1)"
79
+ [[ -n "$BACKUP_DIR" ]] || { err "No backup directory found inside archive."; rm -rf "$STAGE"; exit 1; }
80
+ log "Extracted to: $BACKUP_DIR"
81
+ else
82
+ err "Backup not found: $BACKUP_ARG (need a directory, .zip, or .7z)"
83
+ exit 1
84
+ fi
85
+ [[ -d "$BACKUP_DIR" ]] || { err "Backup directory not found: $BACKUP_DIR"; exit 1; }
86
+
87
+ # ─── Confirm ─────────────────────────────────────────────────────
88
+ echo
89
+ warn "════════════════════════════════════════════════════════════"
90
+ warn " Pi Complete Restore"
91
+ warn "════════════════════════════════════════════════════════════"
92
+ warn "This will OVERWRITE all current Pi configuration."
93
+ warn "Backup source: $BACKUP_DIR"
94
+ warn "Target: $PI_ROOT"
95
+ warn "Pi must NOT be running."
96
+ echo
97
+ read -rp "Continue? [y/N] " -n 1
98
+ echo
99
+ [[ $REPLY =~ ^[Yy]$ ]] || { log "Aborted."; exit 0; }
100
+
101
+ # ─── Helpers ─────────────────────────────────────────────────────
102
+ restore_file() {
103
+ local src="$1" dst="$2"
104
+ if [[ -f "$src" ]]; then
105
+ mkdir -p "$(dirname "$dst")"
106
+ cp "$src" "$dst"
107
+ ok " $(basename "$src")"
108
+ else
109
+ warn " $(basename "$src") — not in backup, skipping"
110
+ fi
111
+ }
112
+
113
+ restore_dir() {
114
+ local src="$1" dst="$2"
115
+ if [[ -d "$src" ]]; then
116
+ mkdir -p "$(dirname "$dst")"
117
+ rm -rf "$dst"
118
+ cp -a "$src" "$dst"
119
+ ok " $(basename "$src")/"
120
+ else
121
+ warn " $(basename "$src")/ — not in backup, skipping"
122
+ fi
123
+ }
124
+
125
+ # ─── Main ────────────────────────────────────────────────────────
126
+ main() {
127
+ log "════════════════════════════════════════════════════════════"
128
+ log " Restoring Pi from backup"
129
+ log "════════════════════════════════════════════════════════════"
130
+ echo
131
+
132
+ # ── 1. Core Config Files ──────────────────────────────────────
133
+ log "1/8 Core configuration files..."
134
+ restore_file "$BACKUP_DIR/settings.json" "$PI_ROOT/agent/settings.json"
135
+ restore_file "$BACKUP_DIR/auth.json" "$PI_ROOT/agent/auth.json"
136
+ restore_file "$BACKUP_DIR/models.json" "$PI_ROOT/agent/models.json"
137
+ restore_file "$BACKUP_DIR/trust.json" "$PI_ROOT/agent/trust.json"
138
+ restore_file "$BACKUP_DIR/keybindings.json" "$PI_ROOT/agent/keybindings.json"
139
+ restore_file "$BACKUP_DIR/pix.json" "$PI_ROOT/agent/pix.json"
140
+ restore_file "$BACKUP_DIR/optimizer.json" "$PI_ROOT/agent/optimizer.json"
141
+ restore_file "$BACKUP_DIR/lmstudio.json.bak" "$PI_ROOT/agent/lmstudio.json.bak"
142
+ restore_file "$BACKUP_DIR/hermes-memory-config.json" "$PI_ROOT/agent/hermes-memory-config.json"
143
+ restore_file "$BACKUP_DIR/pi.code-workspace" "$PI_ROOT/agent/pi.code-workspace"
144
+
145
+ # ── 2. Extension Configs ──────────────────────────────────────
146
+ log "2/8 Extension configurations..."
147
+ restore_file "$BACKUP_DIR/pi-hermes-memory-cleanup/scoped-memory.json" \
148
+ "$PI_ROOT/agent/pi-hermes-memory-cleanup/scoped-memory.json"
149
+ restore_file "$BACKUP_DIR/loop-police.json" "$PI_ROOT/agent/loop-police.json"
150
+ restore_file "$BACKUP_DIR/pi-rtk/config.json" "$PI_ROOT/agent/pi-rtk/config.json"
151
+ restore_file "$BACKUP_DIR/context-mode/config.json" "$PI_ROOT/agent/context-mode/config.json"
152
+ restore_file "$BACKUP_DIR/pi-timeout/config.json" "$PI_ROOT/agent/pi-timeout/config.json"
153
+ restore_file "$BACKUP_DIR/pi-context-viewer/config.json" "$PI_ROOT/agent/pi-context-viewer/config.json"
154
+ restore_file "$BACKUP_DIR/pix-optimizer/config.json" "$PI_ROOT/agent/pix-optimizer/config.json"
155
+ restore_file "$BACKUP_DIR/pi-nvidia-nim/config.json" "$PI_ROOT/agent/pi-nvidia-nim/config.json"
156
+
157
+ # ── 3. Global Skills ──────────────────────────────────────────
158
+ log "3/8 Global skills..."
159
+ restore_dir "$BACKUP_DIR/skills" "$PI_ROOT/agent/skills"
160
+
161
+ # ── 4. Project Skills & Memory ────────────────────────────────
162
+ log "4/8 Project skills & memory..."
163
+ if [[ -d "$BACKUP_DIR/projects-memory" ]]; then
164
+ for proj in "$BACKUP_DIR/projects-memory"/*/; do
165
+ [[ -d "$proj" ]] || continue
166
+ name="$(basename "$proj")"
167
+ restore_dir "$proj" "$PI_ROOT/agent/projects-memory/$name"
168
+ done
169
+ ok " projects-memory/ restored"
170
+ else
171
+ warn " No project memory in backup"
172
+ fi
173
+
174
+ # ── 5. Local Extensions ───────────────────────────────────────
175
+ log "5/8 Local extension source code..."
176
+ if [[ -d "$BACKUP_DIR/local-extensions" ]]; then
177
+ for ext in "$BACKUP_DIR/local-extensions"/*/; do
178
+ [[ -d "$ext" ]] || continue
179
+ name="$(basename "$ext")"
180
+ mkdir -p "$HOME/projects/personal/$name"
181
+ rsync -a "$ext/" "$HOME/projects/personal/$name/"
182
+ ok " $name/ restored"
183
+ done
184
+ else
185
+ warn " No local extensions in backup"
186
+ fi
187
+
188
+ # ── 6. npm Extension Manifests ────────────────────────────────
189
+ log "6/8 npm extension manifests..."
190
+ if [[ -d "$BACKUP_DIR/npm-manifests" ]]; then
191
+ mkdir -p "$PI_ROOT/agent/npm"
192
+ cp -a "$BACKUP_DIR/npm-manifests/"* "$PI_ROOT/agent/npm/" 2>/dev/null || true
193
+ ok " npm manifests restored (run 'npm install' or 'bun install' to reinstall deps)"
194
+ else
195
+ warn " No npm manifests in backup"
196
+ fi
197
+
198
+ # ── 7. Hermes Memory ──────────────────────────────────────────
199
+ log "7/8 Hermes memory (active files)..."
200
+ if [[ -d "$BACKUP_DIR/pi-hermes-memory" ]]; then
201
+ mkdir -p "$PI_ROOT/agent/pi-hermes-memory"
202
+ for f in MEMORY.md USER.md failures.md retired-failures.md retired-memory.md; do
203
+ restore_file "$BACKUP_DIR/pi-hermes-memory/$f" "$PI_ROOT/agent/pi-hermes-memory/$f"
204
+ done
205
+ else
206
+ warn " No Hermes memory in backup"
207
+ fi
208
+
209
+ # ── 8. Cleanup Backups ────────────────────────────────────────
210
+ log "8/8 Hermes-cleanup backups..."
211
+ if [[ -d "$BACKUP_DIR/hermes-cleanup" ]]; then
212
+ mkdir -p "$PI_ROOT/agent/hermes-cleanup"
213
+ for d in "$BACKUP_DIR/hermes-cleanup"/*/; do
214
+ [[ -d "$d" ]] || continue
215
+ name="$(basename "$d")"
216
+ restore_dir "$d" "$PI_ROOT/agent/hermes-cleanup/$name"
217
+ done
218
+ else
219
+ warn " No cleanup backups in backup"
220
+ fi
221
+
222
+ # ── Verify ────────────────────────────────────────────────────
223
+ echo
224
+ log "════════════════════════════════════════════════════════════"
225
+ ok "Restore complete!"
226
+ echo
227
+ log "Next steps:"
228
+ log " 1. Reinstall npm extension dependencies:"
229
+ log " cd ~/.pi/agent && npm install # or bun install"
230
+ log " 2. Verify config:"
231
+ log " pi --version"
232
+ log " /ctx-doctor"
233
+ log " /loop-police-status"
234
+ log " /scoped-memory-status"
235
+ log " 3. Restart Pi and verify everything works"
236
+ echo
237
+ }
238
+
239
+ main "$@"
240
+
241
+ # ─── Cleanup ────────────────────────────────────────────────────
242
+ if [[ -n "${STAGE:-}" && -d "$STAGE" ]]; then
243
+ rm -rf "$STAGE"
244
+ log "Removed temp extraction dir."
245
+ fi