pi-scenes 0.2.0 → 0.2.2

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
@@ -127,6 +127,8 @@ In short: **installed forever, loaded per scene**. One caveat: extensions you in
127
127
 
128
128
  - Same spec in `common` and a scene → deduplicated at the union (loaded once).
129
129
  - Same package, different spellings (e.g. `npm:x@1.0.3` vs bare `npm:x`) → identity-level dedupe keeps the first (common > scene); a manually pinned variant in settings is treated as `borrowed` (no duplicate injection). `/scene` and `/scene status` warn about such spelling mismatches so you can unify them.
130
+ - **Local path vs npm spelling (v0.2.1):** if settings already loads a package via a local path (e.g. your dev checkout `/Users/you/dev/pi-carryover`), a scene preset listing `npm:pi-carryover` will *borrow* your entry instead of installing — `pi install` is skipped so a second spelling is never appended to settings. This matters because two spellings of one package = the same extension loaded twice = pi exits at startup with a tool-name conflict. If settings ever ends up in that state (e.g. a manual `pi install` added the duplicate), `/scene` refuses to switch and tells you exactly which entries to delete.
131
+ - **Hotfix (v0.2.2):** `/scene <name>` crashed with `Extension "command:scene" error: target.filter is not a function` — the v0.2.1 refactor passed `computeTarget()`'s `{packages, skills}` object where the old call site used `target.packages`. Any scene switch hit this. Fixed, plus a command-layer smoke test (isolated via `PI_SCENES_DIR` + dynamic import in a dedicated test file, because the extension's default export resolves its base dir at module-evaluation time — a static import in tests would read/write your real `~/.pi/agent`).
130
132
 
131
133
  ## How it works
132
134
 
@@ -169,7 +171,7 @@ Before uninstalling, `/scene off` and prune entries you don't want to keep from
169
171
 
170
172
  ```bash
171
173
  git clone https://github.com/Feng-H/pi-scenes && cd pi-scenes
172
- npm test # node:test, 14 cases: injection/reclaim + usage/evolution + conflict guards (no TUI needed)
174
+ npm test # node:test, 18 cases: injection/reclaim + usage/evolution + conflict guards + command-layer smoke (no TUI needed)
173
175
  ```
174
176
 
175
177
  Tests isolate via the `PI_SCENES_DIR` env var — your real `~/.pi/agent` is never touched.
@@ -307,6 +309,8 @@ pi install git:github.com/Feng-H/pi-scenes
307
309
 
308
310
  - 同一写法在 common 与场景重复 → 并集时去重,只加载一次。
309
311
  - 同包异写法(如 `npm:x@1.0.3` 与裸名 `npm:x`)→ 身份级去重取首个(common 优先);settings 里手动 pin 的异写法条目视为 `borrowed`,不重复注入。`/scene` 切换与 `/scene status` 会对异写法发出提醒,建议统一。
312
+ - **本地路径与 npm 写法并存(v0.2.1):** 若 settings 已通过本地路径加载某包(如你的开发目录 `/Users/you/dev/pi-carryover`),场景预设里的 `npm:pi-carryover` 会*借用*你的条目而不安装——跳过 `pi install`,绝不向 settings 追加第二种写法。这一点很关键:同一包双写法 = 同一扩展被加载两份 = pi 启动时工具重名冲突退出。若 settings 已陷入该状态(如手动 `pi install` 造成的重复),`/scene` 会拒绝切换并明确告知需要删除哪个条目。
313
+ - **热修(v0.2.2):** `/scene <name>` 曾报 `Extension "command:scene" error: target.filter is not a function`——v0.2.1 重构时把 `computeTarget()` 返回的 `{packages, skills}` 对象传给了旧调用点用 `target.packages` 的地方,任何场景切换必触发。已修复,并补 command 层冒烟测试(独立测试文件 + `PI_SCENES_DIR` 环境变量 + 动态 import 隔离:扩展 default export 在模块求值时解析 base 目录,测试里静态 import 会直接读写真实 `~/.pi/agent`)。
310
314
 
311
315
  ## 工作原理
312
316
 
@@ -349,7 +353,7 @@ managed 注入的条目在卸载前建议先 `/scene off` + 手工清理 `packag
349
353
 
350
354
  ```bash
351
355
  git clone https://github.com/Feng-H/pi-scenes && cd pi-scenes
352
- npm test # node:test,14 用例:注入/回收 + 用量/进化 + 异写法冲突防护(无需 TUI)
356
+ npm test # node:test,18 用例:注入/回收 + 用量/进化 + 异写法冲突防护 + command 层冒烟(无需 TUI)
353
357
  ```
354
358
 
355
359
  测试用 `PI_SCENES_DIR` 环境变量隔离基目录,不碰真实 `~/.pi/agent`。
@@ -676,6 +676,36 @@ export function makeCore(baseDir: string) {
676
676
  return out;
677
677
  }
678
678
 
679
+ /**
680
+ * 身份级缺失判定:磁盘已装,或 settings 已手配同包另一种写法(切换时按 borrowed 借用)。
681
+ * 必须在 pi install 之前做:否则 pi install 会向 settings 追加第二种写法,
682
+ * 同一扩展被加载两份 → pi 启动时工具重名冲突退出。
683
+ */
684
+ function findMissingPackages(target: PackageEntry[], settingsPackages: PackageEntry[]): PackageEntry[] {
685
+ return target.filter((e) => !isPackageInstalled(e) && !settingsPackages.some((x) => sameResource(x, e)));
686
+ }
687
+
688
+ /**
689
+ * 检测 settings.json 里同一包多种写法并存(如本地路径 + npm 写法)。
690
+ * 该状态会让 pi 启动时加载同一扩展两份、工具重名冲突退出,切换前必须拦截。
691
+ */
692
+ function findSettingsDuplicates(pkgs: PackageEntry[]): Array<{ id: string; entries: string[] }> {
693
+ const out: Array<{ id: string; entries: string[] }> = [];
694
+ for (let i = 0; i < pkgs.length; i++) {
695
+ for (let j = i + 1; j < pkgs.length; j++) {
696
+ if (!sameResource(pkgs[i], pkgs[j])) continue;
697
+ const id = packageIdentity(pkgs[i])[0] ?? specOf(pkgs[i]);
698
+ let found = out.find((x) => x.id === id);
699
+ if (!found) {
700
+ found = { id, entries: [] };
701
+ out.push(found);
702
+ }
703
+ for (const s of [specOf(pkgs[i]), specOf(pkgs[j])]) if (!found.entries.includes(s)) found.entries.push(s);
704
+ }
705
+ }
706
+ return out;
707
+ }
708
+
679
709
  /** 静态扫描安装目录,建 tool/command → 包名归因表 */
680
710
  function buildToolMap(): { tools: Map<string, string>; commands: Map<string, string> } {
681
711
  const tools = new Map<string, string>();
@@ -818,6 +848,8 @@ export function makeCore(baseDir: string) {
818
848
  resolveScene,
819
849
  computeTarget,
820
850
  isPackageInstalled,
851
+ findMissingPackages,
852
+ findSettingsDuplicates,
821
853
  applyToSettings,
822
854
  findSpecCollisions,
823
855
  beginSession,
@@ -860,8 +892,25 @@ export default function (pi: ExtensionAPI) {
860
892
  );
861
893
  }
862
894
 
895
+ const prePkgs = (readJson<Record<string, unknown>>(core.paths.settingsFile, {}).packages ?? []) as PackageEntry[];
896
+
897
+ // settings 已有同包异写法并存(无论是否本扩展造成):pi 启动会加载同一扩展两份、
898
+ // 工具重名冲突退出 —— 先拦截并给出修复指引,避免加重坏状态
899
+ const dups = core.findSettingsDuplicates(prePkgs);
900
+ if (dups.length > 0) {
901
+ ctx.ui.notify(
902
+ `⚠️ settings.json 里同一包存在多种写法并存,pi 启动时会因工具重名冲突而退出,请先手动删除其中一种:\n${dups
903
+ .map((d) => ` · ${d.id}: ${d.entries.join(" | ")}`)
904
+ .join("\n")}\n修复后再切换场景`,
905
+ "error",
906
+ );
907
+ return;
908
+ }
909
+
863
910
  // 缺失包:确认后逐个 pi install(全局 scope pi 不自动装,必须显式装)
864
- const missing = target.packages.filter((e) => !core.isPackageInstalled(e));
911
+ // 身份级判定:settings 已手配同包异写法(如本地路径)→ 视为可用,跳过安装;
912
+ // 否则 pi install 会向 settings 追加第二种写法 → 同一扩展加载两份 → pi 启动冲突退出
913
+ const missing = core.findMissingPackages(target.packages, prePkgs);
865
914
  let preInstallPackages: PackageEntry[] | undefined;
866
915
  if (missing.length > 0) {
867
916
  const ok = await ctx.ui.confirm(
@@ -872,7 +921,7 @@ export default function (pi: ExtensionAPI) {
872
921
  ctx.ui.notify("已取消切换(未做任何修改)", "info");
873
922
  return;
874
923
  }
875
- preInstallPackages = readJson<Record<string, unknown>>(core.paths.settingsFile, {}).packages as PackageEntry[] | undefined ?? [];
924
+ preInstallPackages = prePkgs;
876
925
  for (const m of missing) {
877
926
  const r = spawnSync("pi", ["install", specOf(m)], { stdio: "inherit" });
878
927
  if (r.status !== 0) {
@@ -880,7 +929,8 @@ export default function (pi: ExtensionAPI) {
880
929
  return;
881
930
  }
882
931
  }
883
- const still = target.packages.filter((e) => !core.isPackageInstalled(e));
932
+ const postPkgs = (readJson<Record<string, unknown>>(core.paths.settingsFile, {}).packages ?? []) as PackageEntry[];
933
+ const still = core.findMissingPackages(target.packages, postPkgs);
884
934
  if (still.length > 0) {
885
935
  ctx.ui.notify(`仍有包未安装:${still.map(specOf).join(", ")},已中止切换`, "error");
886
936
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-scenes",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "pi 扩展:场景切换器 + 资源自进化。通用层+分场景层打包 extension/skill,/scene 热切换;按实际用量(工具调用归因 + 会话结束 LLM 反思)生成吸收/淘汰提案,让场景定义随使用习惯进化。",
5
5
  "keywords": [
6
6
  "pi-package"