@zew1me/pi-skills-patch 0.0.0-stage → 0.1.0-alpha.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.
Files changed (37) hide show
  1. package/README.md +11 -2
  2. package/dist/ATTRIBUTION.md +320 -0
  3. package/dist/LICENSE +21 -0
  4. package/dist/patches/pi-1.0.3/baseline.absent +2 -0
  5. package/dist/patches/pi-1.0.3/baseline.sha256 +8 -0
  6. package/dist/patches/pi-1.0.3/patched.sha256 +10 -0
  7. package/dist/patches/pi-1.0.3/pre-expansion-patched.sha256 +10 -0
  8. package/dist/patches/pi-1.0.3/pre-expansion-upgrade.patch +339 -0
  9. package/dist/patches/pi-1.0.3/skills.patch +1132 -0
  10. package/dist/patches/pi-1.0.4/baseline.absent +2 -0
  11. package/dist/patches/pi-1.0.4/baseline.sha256 +8 -0
  12. package/dist/patches/pi-1.0.4/patched.sha256 +10 -0
  13. package/dist/patches/pi-1.0.4/pre-expansion-patched.sha256 +10 -0
  14. package/dist/patches/pi-1.0.4/pre-expansion-upgrade.patch +339 -0
  15. package/dist/patches/pi-1.0.4/skills.patch +1132 -0
  16. package/dist/patches/pi-1.1.0/baseline.absent +2 -0
  17. package/dist/patches/pi-1.1.0/baseline.sha256 +8 -0
  18. package/dist/patches/pi-1.1.0/patched.sha256 +10 -0
  19. package/dist/patches/pi-1.1.0/pre-expansion-patched.sha256 +10 -0
  20. package/dist/patches/pi-1.1.0/pre-expansion-upgrade.patch +339 -0
  21. package/dist/patches/pi-1.1.0/skills.patch +1138 -0
  22. package/dist/patches/pi-1.3.0/baseline.absent +2 -0
  23. package/dist/patches/pi-1.3.0/baseline.sha256 +8 -0
  24. package/dist/patches/pi-1.3.0/patched.sha256 +10 -0
  25. package/dist/patches/pi-1.3.0/pre-expansion-patched.sha256 +10 -0
  26. package/dist/patches/pi-1.3.0/pre-expansion-upgrade.patch +339 -0
  27. package/dist/patches/pi-1.3.0/skills.patch +1138 -0
  28. package/dist/patches/pi-1.4.0/baseline.absent +2 -0
  29. package/dist/patches/pi-1.4.0/baseline.sha256 +8 -0
  30. package/dist/patches/pi-1.4.0/patched.sha256 +10 -0
  31. package/dist/patches/pi-1.4.0/skills.patch +1138 -0
  32. package/dist/patches/pi-1.5.2/baseline.absent +2 -0
  33. package/dist/patches/pi-1.5.2/baseline.sha256 +8 -0
  34. package/dist/patches/pi-1.5.2/patched.sha256 +10 -0
  35. package/dist/patches/pi-1.5.2/skills.patch +1138 -0
  36. package/dist/scripts/install-extensions.sh +454 -0
  37. package/package.json +31 -4
@@ -0,0 +1,454 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ ROOT_DIR=$(cd "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/.." && pwd)
5
+ AGENT_DIR=${PI_AGENT_DIR:-"${PI_CODING_AGENT_DIR:-$HOME/.pi/agent}"}
6
+ EXTENSION_DIR="$AGENT_DIR/extensions"
7
+ APPLY_SKILLS_PATCH=1
8
+ INSTALL_EXTENSIONS=1
9
+ EXTENSIONS=(clear effort markdown-backlinks subagents)
10
+ # The standalone npm package exposes this script as a `pi-skills-patch` bin symlink.
11
+ # Resolve data paths from BASH_SOURCE but use the invocation name to default to patch-only mode.
12
+ if [[ "$(basename "$0")" == "pi-skills-patch" ]]; then
13
+ set -- --skip-extensions "$@"
14
+ fi
15
+ PATCH_FILES=()
16
+ UPGRADE_SUMS=()
17
+ UPGRADE_PATCHES=()
18
+ UPGRADE_ABSENT=()
19
+ PATCH_STAGE_DIR=
20
+ PATCH_BACKUP_DIR=
21
+ PATCH_COMMIT_IN_PROGRESS=0
22
+ PATCH_APPLIED=()
23
+ EXTENSION_STAGE_DIR=
24
+ EXTENSION_TARGET=
25
+ EXTENSION_BACKUP=
26
+ EXTENSION_COMMIT_IN_PROGRESS=0
27
+
28
+ restore_applied_files() {
29
+ local restored
30
+ for restored in "${PATCH_APPLIED[@]}"; do
31
+ if [[ -f "$PATCH_BACKUP_DIR/$restored" ]]; then
32
+ cp -p "$PATCH_BACKUP_DIR/$restored" "$PI_PACKAGE_DIR/$restored" \
33
+ || printf 'Could not restore %s\n' "$PI_PACKAGE_DIR/$restored" >&2
34
+ else
35
+ rm -f "$PI_PACKAGE_DIR/$restored" \
36
+ || printf 'Could not remove %s during rollback\n' "$PI_PACKAGE_DIR/$restored" >&2
37
+ fi
38
+ done
39
+ }
40
+
41
+ cleanup() {
42
+ local status=$?
43
+ trap - EXIT INT TERM HUP
44
+ if ((PATCH_COMMIT_IN_PROGRESS)); then
45
+ printf 'Interrupted while applying /skills patch; restoring replaced files.\n' >&2
46
+ restore_applied_files
47
+ fi
48
+ if ((EXTENSION_COMMIT_IN_PROGRESS)); then
49
+ if [[ ! -e "$EXTENSION_TARGET" && ! -L "$EXTENSION_TARGET" && (-e "$EXTENSION_BACKUP" || -L "$EXTENSION_BACKUP") ]]; then
50
+ printf 'Interrupted while installing an extension; restoring %s.\n' "$EXTENSION_TARGET" >&2
51
+ mv "$EXTENSION_BACKUP" "$EXTENSION_TARGET" || true
52
+ fi
53
+ fi
54
+ [[ -z "$EXTENSION_STAGE_DIR" ]] || rm -rf "$EXTENSION_STAGE_DIR" || true
55
+ [[ -z "$EXTENSION_BACKUP" ]] || rm -rf "$EXTENSION_BACKUP" || true
56
+ [[ -z "$PATCH_STAGE_DIR" ]] || rm -rf "$PATCH_STAGE_DIR" || true
57
+ [[ -z "$PATCH_BACKUP_DIR" ]] || rm -rf "$PATCH_BACKUP_DIR" || true
58
+ exit "$status"
59
+ }
60
+ trap 'exit 130' INT
61
+ trap 'exit 143' TERM HUP
62
+ trap cleanup EXIT
63
+
64
+ check_legacy_extension_file() {
65
+ local extension=$1 suffix=$2 legacy_file legacy_source
66
+ legacy_file="$EXTENSION_DIR/$extension$suffix"
67
+ [[ -e "$legacy_file" || -L "$legacy_file" ]] || return 0
68
+ if [[ "$suffix" == .ts ]]; then
69
+ legacy_source="$ROOT_DIR/extensions/$extension/index.ts"
70
+ else
71
+ legacy_source="$ROOT_DIR/extensions/$extension/index.test.mjs"
72
+ fi
73
+ if [[ -L "$legacy_file" || ! -f "$legacy_file" || ! -f "$legacy_source" ]] \
74
+ || ! cmp -s "$legacy_file" "$legacy_source"; then
75
+ printf 'Cannot retire %s: it is not a recognized managed legacy extension file. Move it outside the extensions directory after reviewing it, then retry.\n' "$legacy_file" >&2
76
+ return 1
77
+ fi
78
+ }
79
+
80
+ sha256() {
81
+ if command -v shasum > /dev/null; then
82
+ shasum -a 256 "$1" | awk '{print $1}'
83
+ elif command -v sha256sum > /dev/null; then
84
+ sha256sum "$1" | awk '{print $1}'
85
+ else
86
+ printf 'A SHA-256 utility (shasum or sha256sum) is required.\n' >&2
87
+ return 1
88
+ fi
89
+ }
90
+
91
+ manifest_checksum() {
92
+ awk -v file="$2" '$2 == file { print $1 }' "$1"
93
+ }
94
+
95
+ matches_checksum() {
96
+ [[ -f "$2" && "$(sha256 "$2")" == "$1" ]]
97
+ }
98
+
99
+ is_pi_package() {
100
+ [[ -f "$1/package.json" ]] && node -e '
101
+ try {
102
+ const manifest = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"));
103
+ process.exit(manifest.name === "@earendil-works/pi-coding-agent" ? 0 : 1);
104
+ }
105
+ catch { process.exit(1); }
106
+ ' "$1/package.json"
107
+ }
108
+
109
+ is_managed_launcher() {
110
+ local launcher=$1 managed_root=$2
111
+ # Inspect only: never source or execute a launcher to discover its package.
112
+ if [[ -f "$launcher" ]] && grep -Fq 'PI_MANAGED_INSTALL_ROOT=' "$launcher" \
113
+ && grep -Fq '/install/current-version' "$launcher"; then
114
+ return 0
115
+ fi
116
+ [[ -f "$managed_root/managed-install.json" ]] && node -e '
117
+ try {
118
+ const marker = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8").replace(/^\uFEFF/, ""));
119
+ process.exit(marker.kind === "pi-managed-install" && marker.schemaVersion === 1 && marker.layout === "releases-v1" ? 0 : 1);
120
+ }
121
+ catch { process.exit(1); }
122
+ ' "$managed_root/managed-install.json"
123
+ }
124
+
125
+ find_pi_package() {
126
+ local path package_dir managed_root managed_version
127
+ path=$(realpath "$1" 2> /dev/null) || return 1
128
+ # Managed launchers live beside install/, rather than inside the active package.
129
+ # Recognize them positively, even when install/ is missing; an unrelated sibling
130
+ # install path must not hijack ordinary wrapper/shim discovery.
131
+ managed_root="$(dirname "$path")/../install"
132
+ if is_managed_launcher "$path" "$managed_root"; then
133
+ if [[ -f "$managed_root/current-version" ]]; then
134
+ managed_version=$(< "$managed_root/current-version")
135
+ if [[ "$managed_version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$ ]]; then
136
+ package_dir="$managed_root/releases/$managed_version/node_modules/@earendil-works/pi-coding-agent"
137
+ if is_pi_package "$package_dir" && node -e '
138
+ try {
139
+ const manifest = JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"));
140
+ process.exit(manifest.version === process.argv[2] ? 0 : 1);
141
+ }
142
+ catch { process.exit(1); }
143
+ ' "$package_dir/package.json" "$managed_version"; then
144
+ printf '%s\n' "$(realpath "$package_dir")"
145
+ return 0
146
+ fi
147
+ fi
148
+ fi
149
+ printf 'Could not locate the active managed Pi package under %s. Repair the managed install or set PI_PACKAGE_DIR.\n' "$managed_root" >&2
150
+ return 2
151
+ fi
152
+ # Homebrew's wrapper lives in <formula>/bin while the package is nested under
153
+ # <formula>/libexec/lib/node_modules. Check that layout before walking parents.
154
+ for package_dir in \
155
+ "$(dirname "$path")/../libexec/lib/node_modules/@earendil-works/pi-coding-agent" \
156
+ "$(dirname "$path")/../lib/node_modules/@earendil-works/pi-coding-agent"; do
157
+ if is_pi_package "$package_dir"; then
158
+ printf '%s\n' "$(realpath "$package_dir")"
159
+ return 0
160
+ fi
161
+ done
162
+ path=$(dirname "$path")
163
+ while [[ "$path" != / ]]; do
164
+ if is_pi_package "$path"; then
165
+ printf '%s\n' "$path"
166
+ return 0
167
+ fi
168
+ path=$(dirname "$path")
169
+ done
170
+ return 1
171
+ }
172
+
173
+ find_global_pi_package() {
174
+ local npm_root package_dir
175
+ command -v npm > /dev/null || return 1
176
+ npm_root=$(npm root --global 2> /dev/null) || return 1
177
+ package_dir="$npm_root/@earendil-works/pi-coding-agent"
178
+ is_pi_package "$package_dir" || return 1
179
+ printf '%s\n' "$package_dir"
180
+ }
181
+
182
+ for arg in "$@"; do
183
+ case "$arg" in
184
+ --skip-skill-loading-patch) APPLY_SKILLS_PATCH=0 ;;
185
+ --skip-extensions) INSTALL_EXTENSIONS=0 ;;
186
+ -h | --help)
187
+ if ((!INSTALL_EXTENSIONS)); then
188
+ printf 'Usage: pi-skills-patch [--help]\n'
189
+ printf ' Apply only the version-checked /skills runtime patch to the installed Pi package.\n'
190
+ else
191
+ printf 'Usage: %s [--skip-skill-loading-patch] [--skip-extensions]\n' "$(basename "$0")"
192
+ printf ' --skip-extensions apply only the /skills patch.\n'
193
+ fi
194
+ exit 0
195
+ ;;
196
+ *)
197
+ printf 'Unknown option: %s\n' "$arg" >&2
198
+ exit 2
199
+ ;;
200
+ esac
201
+ done
202
+
203
+ if ((!INSTALL_EXTENSIONS && !APPLY_SKILLS_PATCH)); then
204
+ printf 'Both the patch and extensions were skipped; nothing to install.\n' >&2
205
+ exit 2
206
+ fi
207
+
208
+ if ((INSTALL_EXTENSIONS)); then
209
+ # Pi loads both top-level files and directory entrypoints. Refuse unknown legacy
210
+ # names before patching or replacing any extension directories.
211
+ for extension in "${EXTENSIONS[@]}"; do
212
+ for suffix in .ts .test.mjs; do
213
+ check_legacy_extension_file "$extension" "$suffix"
214
+ done
215
+ done
216
+ for extension in "${EXTENSIONS[@]}"; do
217
+ if [[ ! -f "$ROOT_DIR/extensions/$extension/index.ts" ]]; then
218
+ printf 'Missing packaged extension entrypoint: %s\n' "$ROOT_DIR/extensions/$extension/index.ts" >&2
219
+ exit 1
220
+ fi
221
+ if [[ ! -f "$ROOT_DIR/extensions/$extension/helpers.ts" ]]; then
222
+ printf 'Missing packaged extension helper: %s\n' "$ROOT_DIR/extensions/$extension/helpers.ts" >&2
223
+ exit 1
224
+ fi
225
+ done
226
+ fi
227
+
228
+ if ((APPLY_SKILLS_PATCH)); then
229
+ PI_PACKAGE_DIR=${PI_PACKAGE_DIR:-}
230
+ if [[ -z "$PI_PACKAGE_DIR" ]]; then
231
+ PI_BIN=$(command -v pi || true)
232
+ if [[ -n "$PI_BIN" ]]; then
233
+ # `pi` is commonly a symlink to <package>/dist/cli.js, so find the
234
+ # package from its resolved entry point instead of assuming npm's bin layout.
235
+ discovery_status=0
236
+ PI_PACKAGE_DIR=$(find_pi_package "$PI_BIN") || discovery_status=$?
237
+ if ((discovery_status == 2)); then
238
+ exit 1
239
+ fi
240
+ fi
241
+ # Version managers such as mise can expose `pi` through a shim rather
242
+ # than the package's CLI entrypoint. Fall back to npm's global package root.
243
+ if [[ -z "$PI_PACKAGE_DIR" ]]; then
244
+ PI_PACKAGE_DIR=$(find_global_pi_package || true)
245
+ fi
246
+ fi
247
+ if [[ -z "$PI_PACKAGE_DIR" ]] || ! is_pi_package "$PI_PACKAGE_DIR"; then
248
+ printf 'Could not locate @earendil-works/pi-coding-agent; install extensions only or set PI_PACKAGE_DIR.\n' >&2
249
+ exit 1
250
+ fi
251
+ PI_PACKAGE_DIR=$(realpath "$PI_PACKAGE_DIR")
252
+ PI_VERSION=$(node -p 'require(process.argv[1]).version' "$PI_PACKAGE_DIR/package.json")
253
+ PATCH_DIR="$ROOT_DIR/patches/pi-$PI_VERSION"
254
+ PATCH_FILE="$PATCH_DIR/skills.patch"
255
+ BASELINE_SUMS="$PATCH_DIR/baseline.sha256"
256
+ BASELINE_ABSENT="$PATCH_DIR/baseline.absent"
257
+ PATCHED_SUMS="$PATCH_DIR/patched.sha256"
258
+ if [[ ! -f "$PATCH_FILE" || ! -f "$BASELINE_SUMS" || ! -f "$BASELINE_ABSENT" || ! -f "$PATCHED_SUMS" ]]; then
259
+ printf 'No complete /skills patch exists for pi %s. Use --skip-skill-loading-patch.\n' "$PI_VERSION" >&2
260
+ exit 1
261
+ fi
262
+
263
+ while read -r checksum file extra; do
264
+ if [[ ! "$checksum" =~ ^[0-9a-f]{64}$ || -z "$file" || -n "${extra:-}" || "$file" == /* || "$file" == ".." || "$file" == ../* || "$file" == */../* || "$file" == */.. ]]; then
265
+ printf 'Patched checksum manifest contains an invalid entry.\n' >&2
266
+ exit 1
267
+ fi
268
+ PATCH_FILES+=("$file")
269
+ done < "$PATCHED_SUMS"
270
+ if ((${#PATCH_FILES[@]} == 0)); then
271
+ printf 'Patched checksum manifest is empty.\n' >&2
272
+ exit 1
273
+ fi
274
+
275
+ for upgrade_sums in "$PATCH_DIR"/*-patched.sha256; do
276
+ [[ -f "$upgrade_sums" ]] || continue
277
+ upgrade_patch=${upgrade_sums%-patched.sha256}-upgrade.patch
278
+ if [[ ! -f "$upgrade_patch" ]]; then
279
+ printf 'The /skills upgrade state %s for pi %s is incomplete.\n' "$(basename "$upgrade_sums")" "$PI_VERSION" >&2
280
+ exit 1
281
+ fi
282
+ UPGRADE_SUMS+=("$upgrade_sums")
283
+ UPGRADE_PATCHES+=("$upgrade_patch")
284
+ # Optional, and empty when the state contains every patched file. Lists paths that must not exist in
285
+ # that state, so a patch which adds a file can still describe the states that predate it.
286
+ upgrade_absent=${upgrade_sums%-patched.sha256}-absent
287
+ [[ -f "$upgrade_absent" ]] || upgrade_absent=
288
+ UPGRADE_ABSENT+=("$upgrade_absent")
289
+ done
290
+ for upgrade_patch in "$PATCH_DIR"/*-upgrade.patch; do
291
+ [[ -f "$upgrade_patch" ]] || continue
292
+ upgrade_sums=${upgrade_patch%-upgrade.patch}-patched.sha256
293
+ if [[ ! -f "$upgrade_sums" ]]; then
294
+ printf 'The /skills upgrade state %s for pi %s is incomplete.\n' "$(basename "$upgrade_patch")" "$PI_VERSION" >&2
295
+ exit 1
296
+ fi
297
+ done
298
+
299
+ package_is_baseline=1
300
+ package_is_patched=1
301
+ for file in "${PATCH_FILES[@]}"; do
302
+ patched_checksum=$(manifest_checksum "$PATCHED_SUMS" "$file")
303
+ baseline_checksum=$(manifest_checksum "$BASELINE_SUMS" "$file")
304
+ if [[ ! "$patched_checksum" =~ ^[0-9a-f]{64}$ ]]; then
305
+ printf 'Patched checksum manifest is invalid for %s.\n' "$file" >&2
306
+ exit 1
307
+ fi
308
+ if grep -Fxq "$file" "$BASELINE_ABSENT"; then
309
+ if [[ -n "$baseline_checksum" ]]; then
310
+ printf 'Baseline manifests conflict for %s.\n' "$file" >&2
311
+ exit 1
312
+ fi
313
+ [[ ! -e "$PI_PACKAGE_DIR/$file" && ! -L "$PI_PACKAGE_DIR/$file" ]] || package_is_baseline=0
314
+ else
315
+ if [[ ! "$baseline_checksum" =~ ^[0-9a-f]{64}$ ]]; then
316
+ printf 'Baseline checksum manifest is invalid for %s.\n' "$file" >&2
317
+ exit 1
318
+ fi
319
+ matches_checksum "$baseline_checksum" "$PI_PACKAGE_DIR/$file" || package_is_baseline=0
320
+ fi
321
+ matches_checksum "$patched_checksum" "$PI_PACKAGE_DIR/$file" || package_is_patched=0
322
+ done
323
+
324
+ MATCHED_UPGRADE_PATCH=
325
+ for upgrade_index in "${!UPGRADE_SUMS[@]}"; do
326
+ upgrade_sums=${UPGRADE_SUMS[$upgrade_index]}
327
+ upgrade_absent=${UPGRADE_ABSENT[$upgrade_index]}
328
+ upgrade_matches=1
329
+ for file in "${PATCH_FILES[@]}"; do
330
+ upgrade_checksum=$(manifest_checksum "$upgrade_sums" "$file")
331
+ if [[ -n "$upgrade_absent" ]] && grep -Fxq "$file" "$upgrade_absent"; then
332
+ if [[ -n "$upgrade_checksum" ]]; then
333
+ printf 'Upgrade manifests conflict for %s in %s.\n' "$file" "$(basename "$upgrade_sums")" >&2
334
+ exit 1
335
+ fi
336
+ [[ ! -e "$PI_PACKAGE_DIR/$file" && ! -L "$PI_PACKAGE_DIR/$file" ]] || upgrade_matches=0
337
+ continue
338
+ fi
339
+ if [[ ! "$upgrade_checksum" =~ ^[0-9a-f]{64}$ ]]; then
340
+ printf 'Upgrade checksum manifest %s is invalid for %s.\n' "$(basename "$upgrade_sums")" "$file" >&2
341
+ exit 1
342
+ fi
343
+ matches_checksum "$upgrade_checksum" "$PI_PACKAGE_DIR/$file" || upgrade_matches=0
344
+ done
345
+ if ((upgrade_matches)); then
346
+ if [[ -n "$MATCHED_UPGRADE_PATCH" ]]; then
347
+ printf 'Installed pi %s matches multiple /skills upgrade states; refusing to modify it.\n' "$PI_VERSION" >&2
348
+ exit 1
349
+ fi
350
+ MATCHED_UPGRADE_PATCH=${UPGRADE_PATCHES[$upgrade_index]}
351
+ fi
352
+ done
353
+
354
+ if ((package_is_patched)); then
355
+ APPLY_SKILLS_PATCH=0
356
+ printf '/skills patch for pi %s is already applied.\n' "$PI_VERSION"
357
+ else
358
+ if [[ -n "$MATCHED_UPGRADE_PATCH" ]]; then
359
+ PATCH_TO_APPLY=$MATCHED_UPGRADE_PATCH
360
+ printf 'Upgrading a previously applied /skills patch for pi %s.\n' "$PI_VERSION"
361
+ elif ((!package_is_baseline)); then
362
+ printf 'Installed pi %s does not match this patch baseline; refusing to modify it.\n' "$PI_VERSION" >&2
363
+ exit 1
364
+ else
365
+ PATCH_TO_APPLY="$PATCH_FILE"
366
+ fi
367
+
368
+ command -v patch > /dev/null || {
369
+ printf 'The patch utility is required.\n' >&2
370
+ exit 1
371
+ }
372
+ PATCH_STAGE_DIR=$(mktemp -d "$PI_PACKAGE_DIR/.pi-skills-patch.XXXXXX")
373
+ PATCH_BACKUP_DIR=$(mktemp -d "$PI_PACKAGE_DIR/.pi-skills-backup.XXXXXX")
374
+ for file in "${PATCH_FILES[@]}"; do
375
+ mkdir -p "$(dirname "$PATCH_STAGE_DIR/$file")" "$(dirname "$PATCH_BACKUP_DIR/$file")"
376
+ if [[ -f "$PI_PACKAGE_DIR/$file" ]]; then
377
+ cp -p "$PI_PACKAGE_DIR/$file" "$PATCH_STAGE_DIR/$file"
378
+ cp -p "$PI_PACKAGE_DIR/$file" "$PATCH_BACKUP_DIR/$file"
379
+ fi
380
+ done
381
+ patch --batch --forward --strip=1 --directory="$PATCH_STAGE_DIR" < "$PATCH_TO_APPLY" > /dev/null
382
+ for file in "${PATCH_FILES[@]}"; do
383
+ patched_checksum=$(manifest_checksum "$PATCHED_SUMS" "$file")
384
+ if ! matches_checksum "$patched_checksum" "$PATCH_STAGE_DIR/$file"; then
385
+ printf 'Staged /skills patch produced an unexpected %s.\n' "$file" >&2
386
+ exit 1
387
+ fi
388
+ done
389
+ fi
390
+ fi
391
+
392
+ if ((INSTALL_EXTENSIONS)); then
393
+ mkdir -p "$EXTENSION_DIR"
394
+ for extension in "${EXTENSIONS[@]}"; do
395
+ EXTENSION_STAGE_DIR=$(mktemp -d "$EXTENSION_DIR/.${extension}.XXXXXX")
396
+ while IFS= read -r -d '' source_file; do
397
+ relative_file=${source_file#"$ROOT_DIR/extensions/$extension/"}
398
+ mkdir -p "$EXTENSION_STAGE_DIR/$(dirname "$relative_file")"
399
+ cp "$source_file" "$EXTENSION_STAGE_DIR/$relative_file"
400
+ done < <(find "$ROOT_DIR/extensions/$extension" -type f -name '*.ts' ! -name '*.test.*' -print0)
401
+
402
+ EXTENSION_TARGET="$EXTENSION_DIR/$extension"
403
+ EXTENSION_BACKUP="$EXTENSION_STAGE_DIR.backup"
404
+ EXTENSION_COMMIT_IN_PROGRESS=1
405
+ if [[ -e "$EXTENSION_TARGET" || -L "$EXTENSION_TARGET" ]]; then
406
+ mv "$EXTENSION_TARGET" "$EXTENSION_BACKUP"
407
+ fi
408
+ if ! mv "$EXTENSION_STAGE_DIR" "$EXTENSION_TARGET"; then
409
+ printf 'Could not install %s; restoring its previous extension tree.\n' "$extension" >&2
410
+ rm -rf "$EXTENSION_TARGET"
411
+ if [[ -e "$EXTENSION_BACKUP" || -L "$EXTENSION_BACKUP" ]]; then
412
+ mv "$EXTENSION_BACKUP" "$EXTENSION_TARGET"
413
+ fi
414
+ EXTENSION_COMMIT_IN_PROGRESS=0
415
+ exit 1
416
+ fi
417
+ EXTENSION_STAGE_DIR=
418
+ rm -rf "$EXTENSION_BACKUP"
419
+ EXTENSION_BACKUP=
420
+ EXTENSION_TARGET=
421
+ EXTENSION_COMMIT_IN_PROGRESS=0
422
+ done
423
+
424
+ # Recheck after replacements, immediately before each deletion: another process may have
425
+ # changed a legacy file while the new extension trees were being staged.
426
+ for extension in "${EXTENSIONS[@]}"; do
427
+ for suffix in .ts .test.mjs; do
428
+ check_legacy_extension_file "$extension" "$suffix"
429
+ rm -f "$EXTENSION_DIR/$extension$suffix"
430
+ done
431
+ done
432
+ fi
433
+
434
+ if [[ -n "$PATCH_STAGE_DIR" ]]; then
435
+ PATCH_COMMIT_IN_PROGRESS=1
436
+ for file in "${PATCH_FILES[@]}"; do
437
+ # Record before rename so cleanup can recover even if interrupted immediately after it.
438
+ PATCH_APPLIED+=("$file")
439
+ if ! mv "$PATCH_STAGE_DIR/$file" "$PI_PACKAGE_DIR/$file"; then
440
+ printf 'Could not apply /skills patch; restoring replaced files.\n' >&2
441
+ restore_applied_files
442
+ PATCH_COMMIT_IN_PROGRESS=0
443
+ exit 1
444
+ fi
445
+ done
446
+ PATCH_COMMIT_IN_PROGRESS=0
447
+ printf 'Applied /skills patch for pi %s\n' "$PI_VERSION"
448
+ fi
449
+
450
+ if ((INSTALL_EXTENSIONS)); then
451
+ printf 'Installed pi extensions from %s\n' "$ROOT_DIR/extensions"
452
+ printf 'Destination: %s\n' "$EXTENSION_DIR"
453
+ printf 'Run /reload in an active pi session to load changes.\n'
454
+ fi
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@zew1me/pi-skills-patch",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-alpha.0",
4
+ "type": "module",
5
+ "description": "Version-checked /skills runtime patch installer for supported Pi versions",
6
+ "license": "SEE LICENSE IN dist/LICENSE",
7
+ "keywords": [
8
+ "pi",
9
+ "skills",
10
+ "patch"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/zew1me/pi-buildout.git",
15
+ "directory": "packages/pi-skills-patch"
16
+ },
17
+ "engines": {
18
+ "node": ">=22.19.0"
19
+ },
20
+ "bin": {
21
+ "pi-skills-patch": "dist/scripts/install-extensions.sh"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public",
28
+ "registry": "https://registry.npmjs.org/"
29
+ },
30
+ "scripts": {
31
+ "prepack": "node ../../scripts/stage-release-package.mjs skills-patch"
32
+ }
33
+ }