pi-scenes 0.9.0 → 0.10.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/README.md CHANGED
@@ -55,6 +55,7 @@ Then `/reload` and `/scene` is live.
55
55
  /scene migrate # convert a ≤0.6 legacy global scene into this project's scene
56
56
  /scene init # scaffold scenes.json template + scene skill dirs
57
57
  /scene stats # usage dashboard: sessions, tool calls, reflections
58
+ /scene update-assets # sync preset templates/skills from the package (edits kept, rest updated)
58
59
  /scene evolve # generate & apply evolution proposals (confirm-first)
59
60
  /scene evolve auto # toggle auto-apply at session end (opt-in)
60
61
  ```
@@ -264,6 +265,11 @@ Before uninstalling, `/scene off` and prune entries you don't want to keep from
264
265
 
265
266
  ## Changelog
266
267
 
268
+ ### v0.10.0 (2026-09-23)
269
+
270
+ - **Vendored asset fingerprints + safe updates** — preset templates/skills copied to `~/.pi/agent/scenes/` now record a content-hash baseline (`.assets-manifest.json`). New `/scene update-assets` command syncs package assets to your disk: **untouched files follow new package versions; files you edited are kept yours** (listed as conflicts); presets removed from the package are reported but never auto-deleted. Closes the "npm updates don't update vendored assets" gap — ownership transfer no longer means losing updates
271
+ - Scaffold now runs the same safe sync internally (fills missing + records baselines)
272
+
267
273
  ### v0.9.0 (2026-09-23)
268
274
 
269
275
  - **New preset scene: `learning` 📚** (8th preset) — knowledge internalization, verification, and spaced review. Ships `pi-web-access` (learning materials) + `pi-subagents` (parallel multi-perspective study), three workflow templates (`/feynman` four-step internalization, `/socratic` question-only verification, `/flashcards` Q&A+cloze cards with a spaced-repetition schedule), and a vendored `eli5` skill (analogy-first, zero-jargon first pass, 5yo/12yo/adult three-tier explanations)
@@ -347,6 +353,7 @@ pi install git:github.com/Feng-H/pi-scenes
347
353
  /scene migrate # 把 ≤0.6 遗留的全局场景转为当前项目场景
348
354
  /scene init # 生成模板 scenes.json + 场景 skill 目录骨架
349
355
  /scene stats # 用量仪表盘:会话数 / 工具调用 / 反思评分
356
+ /scene update-assets # 同步包内预置模板/skill(未修改跟随新版,改过的保留)
350
357
  /scene evolve # 生成并应用进化提案(逐条确认)
351
358
  /scene evolve auto # 开关:会话结束自动应用(opt-in)
352
359
  ```
@@ -549,6 +556,11 @@ managed 注入的条目在卸载前建议先 `/scene off` + 手工清理 `packag
549
556
 
550
557
  ## 版本历史
551
558
 
559
+ ### v0.10.0(2026-09-23)
560
+
561
+ - **vendored 资产指纹 + 安全更新**——预置模板/skill 复制到 `~/.pi/agent/scenes/` 时记录内容指纹基线(`.assets-manifest.json`)。新增 `/scene update-assets` 命令同步包内资产到落盘:**你未修改的跟随包新版,你改过的保留你的版本**(列为冲突清单),包内已删除的预设仅提示不自动删。补上「npm 更新不更新 vendored 资产」的缺口——所有权移交不再等于更新断绝
562
+ - scaffold 内部改走同一套安全同步(补缺失 + 记基线)
563
+
552
564
  ### v0.9.0(2026-09-23)
553
565
 
554
566
  - **新增预设场景 `learning` 📚**(第八个)——知识内化、检验、间隔复习。携带 `pi-web-access`(学习材料)+ `pi-subagents`(并行多视角学习)、三个流程模板(`/feynman` 费曼四步内化、`/socratic` 只问不讲的苏格拉底检验、`/flashcards` Q&A+填空双卡型配间隔复习排程)、vendored 技能 `eli5`(类比先行、零术语首过、5 岁/12 岁/成人三档递进)
@@ -76,6 +76,7 @@ import path from "node:path";
76
76
  import os from "node:os";
77
77
  import { spawnSync } from "node:child_process";
78
78
  import { fileURLToPath } from "node:url";
79
+ import { createHash } from "node:crypto";
79
80
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
80
81
 
81
82
  // ── 类型 ────────────────────────────────────────────────────
@@ -429,6 +430,8 @@ export interface CorePaths {
429
430
  projectSettingsFile: string; // <cwd>/.pi/settings.json
430
431
  projectStateFile: string; // <cwd>/.pi/scenes-state.json
431
432
  projectScenesRoot: string; // <cwd>/.pi/scenes
433
+ // v0.10:vendored 资产指纹(包内预设 vs 用户落盘的安全更新基线)
434
+ assetsManifest: string; // <scenesRoot>/.assets-manifest.json
432
435
  }
433
436
 
434
437
  export function makeCore(baseDir: string, projectDir?: string) {
@@ -446,6 +449,7 @@ export function makeCore(baseDir: string, projectDir?: string) {
446
449
  projectSettingsFile: path.join(projDir, "settings.json"),
447
450
  projectStateFile: path.join(projDir, "scenes-state.json"),
448
451
  projectScenesRoot: path.join(projDir, "scenes"),
452
+ assetsManifest: path.join(baseDir, "scenes", ".assets-manifest.json"),
449
453
  };
450
454
 
451
455
  function loadScenes(): ScenesFile {
@@ -981,6 +985,134 @@ export function makeCore(baseDir: string, projectDir?: string) {
981
985
  return { tools, commands };
982
986
  }
983
987
 
988
+ // ── v0.10:vendored 资产指纹与安全更新 ──────────────────
989
+ // 问题:scaffold 只补缺失不更新(copyTreeIfMissing 语义),包升级改了预置资产后
990
+ // 用户落盘永远是旧版,且无感知。方案:manifest 记录「上次同步时的内容 hash」作
991
+ // 为预设基线——用户未改过(落盘==基线)→ 跟随包新版;用户改过 → 保留用户版。
992
+ // 所有权语义:预设层(vendored)≠ 资产层(包)≠ 激活层(settings),移交但不断更。
993
+
994
+ function hashFileContent(file: string): string {
995
+ return createHash("sha256").update(fs.readFileSync(file)).digest("hex");
996
+ }
997
+
998
+ /** 目录指纹:递归收集 [相对路径, 文件hash] 排序后整体 hash(文件集变更即变) */
999
+ function hashDirContent(dir: string): string {
1000
+ const parts: string[] = [];
1001
+ const walk = (d: string, rel: string) => {
1002
+ for (const e of fs.readdirSync(d, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
1003
+ const full = path.join(d, e.name);
1004
+ if (e.isDirectory()) walk(full, `${rel}${e.name}/`);
1005
+ else parts.push(`${rel}${e.name}:${hashFileContent(full)}`);
1006
+ }
1007
+ };
1008
+ walk(dir, "");
1009
+ return createHash("sha256").update(parts.join("\n")).digest("hex");
1010
+ }
1011
+
1012
+ type AssetsManifest = { files: Record<string, string> };
1013
+
1014
+ function loadAssetsManifest(): AssetsManifest {
1015
+ // readJson 返回的就是 { files: {...} } 整体,不能再包一层
1016
+ return readJson<AssetsManifest>(paths.assetsManifest, { files: {} });
1017
+ }
1018
+
1019
+ function saveAssetsManifest(m: AssetsManifest): void {
1020
+ writeJson(paths.assetsManifest, m);
1021
+ }
1022
+
1023
+ /** 包内 vendored 资产全集:键=相对 scenesRoot 路径,值={pkgPath(绝对), isDir, hash} */
1024
+ function enumeratePkgAssets(assetsRoots: { skills: string; prompts: string }): Map<string, { pkgPath: string; isDir: boolean; hash: string }> {
1025
+ const out = new Map<string, { pkgPath: string; isDir: boolean; hash: string }>();
1026
+ const skillsRoot = assetsRoots.skills;
1027
+ if (fs.existsSync(skillsRoot)) {
1028
+ for (const scene of fs.readdirSync(skillsRoot)) {
1029
+ const sceneDir = path.join(skillsRoot, scene);
1030
+ if (!fs.statSync(sceneDir).isDirectory()) continue;
1031
+ for (const skill of fs.readdirSync(sceneDir)) {
1032
+ const pkgPath = path.join(sceneDir, skill);
1033
+ if (!fs.statSync(pkgPath).isDirectory()) continue;
1034
+ if (!fs.existsSync(path.join(pkgPath, "SKILL.md"))) continue; // 与 scaffold 同过滤
1035
+ out.set(`${scene}/skills/${skill}`, { pkgPath, isDir: true, hash: hashDirContent(pkgPath) });
1036
+ }
1037
+ }
1038
+ }
1039
+ const promptsRoot = assetsRoots.prompts;
1040
+ if (fs.existsSync(promptsRoot)) {
1041
+ for (const scene of fs.readdirSync(promptsRoot)) {
1042
+ const sceneDir = path.join(promptsRoot, scene);
1043
+ if (!fs.statSync(sceneDir).isDirectory()) continue;
1044
+ for (const f of fs.readdirSync(sceneDir)) {
1045
+ if (!f.endsWith(".md")) continue;
1046
+ const pkgPath = path.join(sceneDir, f);
1047
+ out.set(`${scene}/prompts/${f}`, { pkgPath, isDir: false, hash: hashFileContent(pkgPath) });
1048
+ }
1049
+ }
1050
+ }
1051
+ return out;
1052
+ }
1053
+
1054
+ type AssetsSyncReport = {
1055
+ added: string[];
1056
+ updated: string[];
1057
+ conflicts: string[];
1058
+ removed: string[];
1059
+ baselineRepaired: string[];
1060
+ };
1061
+
1062
+ /** 同步包内 vendored 资产到用户落盘(安全更新):
1063
+ * added=补缺失;updated=用户未改且包有新版→覆盖;conflicts=用户改过→保留用户版;
1064
+ * removed=manifest 有记录但包内已删→仅提示不删;baselineRepaired=无基线且与包一致→补录。
1065
+ * assetsRoots 可注入(测试用),缺省从本扩展包根推导。 */
1066
+ function syncVendoredAssets(assetsRoots?: { skills: string; prompts: string }): AssetsSyncReport {
1067
+ const report: AssetsSyncReport = { added: [], updated: [], conflicts: [], removed: [], baselineRepaired: [] };
1068
+ let pkgRoot: string;
1069
+ try {
1070
+ pkgRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
1071
+ } catch {
1072
+ return report;
1073
+ }
1074
+ const roots = assetsRoots ?? { skills: path.join(pkgRoot, "assets", "scene-skills"), prompts: path.join(pkgRoot, "assets", "scene-prompts") };
1075
+ const pkgAssets = enumeratePkgAssets(roots);
1076
+ const manifest = loadAssetsManifest();
1077
+ for (const [key, info] of pkgAssets) {
1078
+ const dst = path.join(paths.scenesRoot, key);
1079
+ if (!fs.existsSync(dst)) {
1080
+ fs.cpSync(info.pkgPath, dst, { recursive: true });
1081
+ manifest.files[key] = info.hash;
1082
+ report.added.push(key);
1083
+ continue;
1084
+ }
1085
+ const userHash = info.isDir ? hashDirContent(dst) : hashFileContent(dst);
1086
+ const baseHash = manifest.files[key];
1087
+ if (baseHash === undefined) {
1088
+ // 无基线(manifest 机制引入前的落盘):与包一致→补录基线;不一致→保守冲突
1089
+ if (userHash === info.hash) {
1090
+ manifest.files[key] = info.hash;
1091
+ report.baselineRepaired.push(key);
1092
+ } else {
1093
+ report.conflicts.push(key);
1094
+ }
1095
+ } else if (userHash === baseHash) {
1096
+ // 用户未改:跟随包新版
1097
+ if (userHash !== info.hash) {
1098
+ fs.rmSync(dst, { recursive: true });
1099
+ fs.cpSync(info.pkgPath, dst, { recursive: true });
1100
+ manifest.files[key] = info.hash;
1101
+ report.updated.push(key);
1102
+ }
1103
+ } else {
1104
+ // 用户改过:保留用户版(不动 manifest 基线)
1105
+ report.conflicts.push(key);
1106
+ }
1107
+ }
1108
+ // 包内已删的预设:仅报告(不自动删用户文件)
1109
+ for (const key of Object.keys(manifest.files)) {
1110
+ if (!pkgAssets.has(key)) report.removed.push(key);
1111
+ }
1112
+ saveAssetsManifest(manifest);
1113
+ return report;
1114
+ }
1115
+
984
1116
  /**
985
1117
  * 生成模板 scenes.json + 场景 skill 目录骨架 + 预置精选技能。
986
1118
  * 预设均为真实存在的包(npm/git,2026-09 核验);git 技能包用 object form
@@ -1101,37 +1233,13 @@ export function makeCore(baseDir: string, projectDir?: string) {
1101
1233
  }
1102
1234
  }
1103
1235
  }
1104
- // 预置精选技能:从包内 assets/scene-skills/<场景>/ 复制到场景 skill 目录。
1105
- // 仅在目标不存在时写入(幂等,不覆盖用户已有同名技能);复制后归用户所有,可改可删。
1236
+ // v0.10:vendored 预置(技能+模板)统一走安全同步:补缺失 + 记指纹基线 +
1237
+ // 未修改的跟随包新版(用户改过的保留)。所有权语义见 syncVendoredAssets 注释。
1106
1238
  try {
1107
- const pkgRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
1108
- const assetsRoot = path.join(pkgRoot, "assets", "scene-skills");
1109
- if (fs.existsSync(assetsRoot)) {
1110
- for (const scene of fs.readdirSync(assetsRoot)) {
1111
- const srcScene = path.join(assetsRoot, scene);
1112
- if (!fs.statSync(srcScene).isDirectory()) continue;
1113
- for (const skill of fs.readdirSync(srcScene)) {
1114
- const src = path.join(srcScene, skill);
1115
- const dst = path.join(paths.scenesRoot, scene, "skills", skill);
1116
- if (!fs.existsSync(path.join(src, "SKILL.md")) || fs.existsSync(dst)) continue;
1117
- fs.cpSync(src, dst, { recursive: true });
1118
- created.push(dst);
1119
- }
1120
- }
1121
- }
1122
- // v0.8:预置场景模板 assets/scene-prompts/<场景>/*.md → scenes/<场景>/prompts/(幂等复制,不覆盖用户已有)
1123
- const promptsAssetsRoot = path.join(pkgRoot, "assets", "scene-prompts");
1124
- if (fs.existsSync(promptsAssetsRoot)) {
1125
- for (const scene of fs.readdirSync(promptsAssetsRoot)) {
1126
- const srcScene = path.join(promptsAssetsRoot, scene);
1127
- if (!fs.statSync(srcScene).isDirectory()) continue;
1128
- const dst = path.join(paths.scenesRoot, scene, "prompts");
1129
- const copied = copyTreeIfMissing(srcScene, dst);
1130
- if (copied > 0) created.push(dst);
1131
- }
1132
- }
1239
+ const report = syncVendoredAssets();
1240
+ for (const key of report.added) created.push(path.join(paths.scenesRoot, key));
1133
1241
  } catch {
1134
- // 资产复制失败不阻断 scaffold(如测试环境无 assets)
1242
+ // 资产同步失败不阻断 scaffold(如测试环境无 assets)
1135
1243
  }
1136
1244
  return created;
1137
1245
  }
@@ -1163,6 +1271,7 @@ export function makeCore(baseDir: string, projectDir?: string) {
1163
1271
  endSession,
1164
1272
  buildToolMap,
1165
1273
  scaffold,
1274
+ syncVendoredAssets,
1166
1275
  };
1167
1276
  }
1168
1277
 
@@ -1319,6 +1428,7 @@ export default function (pi: ExtensionAPI) {
1319
1428
  ["migrate", "把 ≤0.6 遗留的全局场景转为当前项目场景"],
1320
1429
  ["init", "生成模板 scenes.json 与 skill 骨架"],
1321
1430
  ["stats", "用量仪表盘(会话/工具调用/反思)"],
1431
+ ["update-assets", "同步包内预置模板/skill(未修改的跟随新版,改过的保留)"],
1322
1432
  ["evolve", "生成并应用进化提案"],
1323
1433
  ["evolve auto", "开关:会话结束自动应用进化"],
1324
1434
  ];
@@ -1399,6 +1509,26 @@ export default function (pi: ExtensionAPI) {
1399
1509
  return;
1400
1510
  }
1401
1511
 
1512
+ if (arg === "update-assets") {
1513
+ const r = core.syncVendoredAssets();
1514
+ const total = r.added.length + r.updated.length + r.conflicts.length + r.removed.length + r.baselineRepaired.length;
1515
+ if (total === 0) {
1516
+ ctx.ui.notify("预置资产已是最新(无新增/更新/冲突)", "info");
1517
+ return;
1518
+ }
1519
+ const lines = [
1520
+ `预置资产同步(包内 assets → ~/.pi/agent/scenes/):`,
1521
+ r.added.length ? ` + 新增 ${r.added.length}:${r.added.join("、")}` : "",
1522
+ r.updated.length ? ` ↻ 更新 ${r.updated.length}(你未修改,已跟随包新版):${r.updated.join("、")}` : "",
1523
+ r.conflicts.length ? ` ⚠️ 冲突 ${r.conflicts.length}(你改过,保留你的版本):${r.conflicts.join("、")}` : "",
1524
+ r.removed.length ? ` ✂️ 包内已移除 ${r.removed.length}(本地保留未删):${r.removed.join("、")}` : "",
1525
+ r.baselineRepaired.length ? ` ∿ 补录基线 ${r.baselineRepaired.length}(首次纳入指纹管理)` : "",
1526
+ ].filter(Boolean);
1527
+ if (r.updated.length > 0) lines.push("已更新模板/skill,/reload 后生效");
1528
+ ctx.ui.notify(lines.join("\n"), "info");
1529
+ return;
1530
+ }
1531
+
1402
1532
  if (arg === "stats") {
1403
1533
  showStats(ctx);
1404
1534
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-scenes",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Scene switcher for pi: bundle extensions, skills, and prompt templates into a common layer + per-scene layers. /scene hot-switches with live reload (project-scoped, zero settings residue); scene-scoped workflow templates arrive via resources_discover with no config writes; scene definitions self-evolve from real usage (tool-call attribution + session-end LLM reflection).",
5
5
  "keywords": [
6
6
  "pi-package"