@danypops/pi-packed 0.21.14 → 0.21.15

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.
@@ -7,9 +7,52 @@
7
7
  */
8
8
  import type { PackageOperation } from "./permission.js";
9
9
 
10
+ /**
11
+ * Packed's own npm package name -- the one target where install/update/
12
+ * remove has a real, confirmed hazard plain /reload cannot fix: this
13
+ * extension's own already-loaded code (e.g. vehicle-target.ts importing
14
+ * @danypops/pi-packed/client) keeps a stale reference to the pre-mutation
15
+ * module even after /reload re-evaluates the extension's own source files.
16
+ * Confirmed live: updating pi-packed mid-session, then /reload, threw
17
+ * "resolveVehicleClientTarget is not a function" -- a brand-new process
18
+ * importing the exact same on-disk files resolved it fine. Node/Bun cache
19
+ * an ES module by resolved file path for the life of the process; an
20
+ * in-place npm overwrite never invalidates an already-loaded entry, and
21
+ * /reload only re-executes the extension's own entry file, not every
22
+ * transitively-cached dependency. Only a full process restart re-imports
23
+ * everything fresh.
24
+ */
25
+ export const PACKED_NPM_PACKAGE_NAME = "@danypops/pi-packed";
26
+
27
+ /**
28
+ * True when a pkg_install/pkg_update source (npm:@danypops/pi-packed,
29
+ * npm:@danypops/pi-packed@1.2.3, git:/https: sources never match) or a
30
+ * pkg_remove bare name refers to Packed's own npm package. Deliberately
31
+ * narrow -- this is the one case Packed can be CERTAIN has the stale-
32
+ * module-cache hazard above, since it's the package whose own code is
33
+ * running the mutation. Any other package's extension could in principle
34
+ * have the same self-import pattern, but Packed has no way to know that
35
+ * about third-party code, so it never guesses there.
36
+ */
37
+ export function targetsPackedItself(sourceOrName: string): boolean {
38
+ const withoutScheme = sourceOrName.startsWith("npm:") ? sourceOrName.slice(4) : sourceOrName;
39
+ const bare = withoutScheme.startsWith("@") ? withoutScheme.split("@").slice(0, 2).join("@") : withoutScheme.split("@")[0];
40
+ return bare === PACKED_NPM_PACKAGE_NAME;
41
+ }
42
+
43
+ export interface ReloadWarningOptions {
44
+ /** True when the target is Packed's own package -- see targetsPackedItself(). */
45
+ restartRequired?: boolean;
46
+ }
47
+
10
48
  /** Inline warning appended to a mutation's pre-confirmation dialog, before
11
49
  * the operation runs -- the moment the user is actually deciding. */
12
- export function reloadWarning(operation: PackageOperation): string {
50
+ export function reloadWarning(operation: PackageOperation, options: ReloadWarningOptions = {}): string {
51
+ if (options.restartRequired) {
52
+ return operation === "remove"
53
+ ? "Packed's own code is already running inside this Pi process -- deactivating it needs a full restart (exit and relaunch Pi), not just /reload."
54
+ : "Packed's own code is already running inside this Pi process -- its updated code can only be picked up by a full restart (exit and relaunch Pi), not just /reload.";
55
+ }
13
56
  return operation === "remove"
14
57
  ? "This will require a Pi reload (/reload) to deactivate it."
15
58
  : "This will likely require a Pi reload (/reload) to activate its resources.";
@@ -35,7 +78,13 @@ export interface ReloadConfirmContext {
35
78
  * shares identical wording and behavior instead of drifting apart --
36
79
  * reload.ts's whole reason for existing.
37
80
  */
38
- export async function confirmReload(ctx: ReloadConfirmContext): Promise<boolean> {
81
+ export async function confirmReload(ctx: ReloadConfirmContext, options: ReloadWarningOptions = {}): Promise<boolean> {
39
82
  if (!ctx.hasUI) return true;
83
+ if (options.restartRequired) {
84
+ return ctx.ui.confirm(
85
+ "Restart Pi now?",
86
+ "Packed's own code changed and cannot be hot-reloaded from inside itself -- exit and relaunch Pi to pick it up. /reload alone will not work.",
87
+ );
88
+ }
40
89
  return ctx.ui.confirm("Reload Pi now?", "This change only takes effect after a reload.");
41
90
  }
@@ -13,6 +13,7 @@ import type { ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-a
13
13
  import { rawKeyHint } from "@earendil-works/pi-coding-agent";
14
14
  import { Input, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
15
15
  import { Card, type Component } from "malevich-tui-components";
16
+ import { targetsPackedItself } from "../approval/reload.js";
16
17
  import { cardTheme } from "../menu-theme.js";
17
18
  import { formatDownloadCount, formatRelativeDate } from "../model.js";
18
19
  import type { Natives, PackageSummary } from "../packed.js";
@@ -32,7 +33,8 @@ export type InstallOutcome = "installed" | "cancelled" | "failed";
32
33
  * path (installPackageWithPolicy) -- most packages aren't daemons at all. */
33
34
  export async function applyInstall(result: PackageSummary, natives: Natives, ctx: ExtensionCommandContext): Promise<InstallOutcome> {
34
35
  const source = `npm:${result.name}`;
35
- const approval = await approvePackageOperation("install", `pi install ${source}`, natives, ctx);
36
+ const restartRequired = targetsPackedItself(result.name);
37
+ const approval = await approvePackageOperation("install", `pi install ${source}`, natives, ctx, restartRequired);
36
38
  if (!approval.allowed) {
37
39
  ctx.ui.notify(approval.message ?? "install denied", "warning");
38
40
  return "cancelled";
@@ -46,7 +48,16 @@ export async function applyInstall(result: PackageSummary, natives: Natives, ctx
46
48
  ctx.ui.notify(`note: could not register a persistent service for ${result.name}: ${e instanceof Error ? e.message : e}`, "warning");
47
49
  }
48
50
  }
49
- ctx.ui.notify(`${output}; reloading Pi resources.`, "info");
51
+ // ctx.reload() re-evaluates this extension's own entry file, same as /reload -- harmless to
52
+ // call either way, but it cannot bust an already-loaded copy of Packed's own client module,
53
+ // so a self-install/update needs the stronger, honest wording instead of implying reload alone
54
+ // finished the job (see approval/reload.ts's targetsPackedItself()).
55
+ ctx.ui.notify(
56
+ restartRequired
57
+ ? `${output}; restart Pi (exit and relaunch) to activate -- /reload alone will not pick up Packed's own updated code.`
58
+ : `${output}; reloading Pi resources.`,
59
+ "info",
60
+ );
50
61
  await ctx.reload();
51
62
  return "installed";
52
63
  } catch (e) {
@@ -1,6 +1,7 @@
1
1
  import type { PackageSummary as Pkg, PackageInfo as PkgInfo } from "@danypops/pi-packed/protocol";
2
2
  import type { AgentToolResult, Theme } from "@earendil-works/pi-coding-agent";
3
3
  import { Text, truncateToWidth } from "@earendil-works/pi-tui";
4
+ import { targetsPackedItself } from "./approval/reload.js";
4
5
  import {
5
6
  TOOL_COLLAPSED_PACKAGE_PREVIEW,
6
7
  TOOL_DETAILS_MAX_CAPABILITIES,
@@ -59,6 +60,11 @@ export interface MutationToolDetails {
59
60
  status: "succeeded" | "cancelled" | "denied";
60
61
  output: string;
61
62
  reloadRequired: boolean;
63
+ /** Optional (not version-gated) -- see approval/reload.ts's targetsPackedItself().
64
+ * Absent on details serialized before this field existed; the renderer treats
65
+ * absent the same as false, so an older persisted tool result still renders
66
+ * exactly as it always did. */
67
+ restartRequired?: boolean;
62
68
  }
63
69
 
64
70
  export type PackageToolDetails = SearchToolDetails | InfoToolDetails | MutationToolDetails;
@@ -181,6 +187,10 @@ export function createMutationDetails(
181
187
  status,
182
188
  output: safeDisplayText(output, TOOL_DETAILS_MAX_OUTPUT_CHARACTERS),
183
189
  reloadRequired,
190
+ // Derived from the raw (pre-sanitized) target -- targetsPackedItself only ever matches a
191
+ // plain npm name/source, never affected by safePackageTarget's URL-credential redaction.
192
+ // Only meaningful when a reload/restart is actually pending at all.
193
+ ...(status === "succeeded" && targetsPackedItself(target) ? { restartRequired: true } : {}),
184
194
  };
185
195
  }
186
196
 
@@ -235,6 +245,7 @@ export function parsePackageToolDetails(value: unknown): PackageToolDetails | un
235
245
  if (typeof candidate.status !== "string" || !MUTATION_STATUSES.has(candidate.status)) return undefined;
236
246
  if (!isShortString(candidate.target) || !isShortString(candidate.output) || typeof candidate.reloadRequired !== "boolean")
237
247
  return undefined;
248
+ if (candidate.restartRequired !== undefined && typeof candidate.restartRequired !== "boolean") return undefined;
238
249
  return value as MutationToolDetails;
239
250
  }
240
251
  } catch {
@@ -312,7 +323,9 @@ export function renderPackageToolResult(
312
323
  const statusColor = details.status === "succeeded" ? "success" : "warning";
313
324
  const lines = [`${theme.fg(statusColor, details.status === "succeeded" ? "✓" : "○")} ${details.operation} ${details.target}`];
314
325
  if (options.expanded && details.output) lines.push(details.output);
315
- if (details.reloadRequired) lines.push(theme.fg("warning", "Reload Pi with /reload to activate the update."));
326
+ if (details.restartRequired)
327
+ lines.push(theme.fg("warning", "Restart Pi (exit and relaunch) to activate -- Packed's own running code can't hot-reload itself."));
328
+ else if (details.reloadRequired) lines.push(theme.fg("warning", "Reload Pi with /reload to activate the update."));
316
329
  return lines.map((line) => truncateToWidth(line, safeWidth));
317
330
  },
318
331
  invalidate() {},
@@ -2,7 +2,7 @@
2
2
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
3
3
  import { Type } from "typebox";
4
4
  import { type PackageOperation, packagePermissionDecision } from "./approval/permission.js";
5
- import { reloadWarning } from "./approval/reload.js";
5
+ import { reloadWarning, targetsPackedItself } from "./approval/reload.js";
6
6
  import { InstallServiceError, type Natives, PI_COMMAND_NAME } from "./packed.js";
7
7
  import {
8
8
  createInfoDetails,
@@ -28,6 +28,10 @@ export async function approvePackageOperation(
28
28
  command: string,
29
29
  natives: Pick<Natives, "security">,
30
30
  ctx: ApprovalContext,
31
+ // True only for a source/name that resolves to Packed's own npm package -- see
32
+ // targetsPackedItself(). Every caller below computes this from the exact string it's
33
+ // about to mutate, so the warning always reflects the real target.
34
+ restartRequired = false,
31
35
  ): Promise<{ allowed: boolean; approved: boolean; reason?: "cancelled" | "denied"; message?: string }> {
32
36
  const settings = await natives.security();
33
37
  const decision = packagePermissionDecision(settings, operation);
@@ -42,7 +46,7 @@ export async function approvePackageOperation(
42
46
  }
43
47
  const approved = await ctx.ui.confirm(
44
48
  `${operation[0]!.toUpperCase()}${operation.slice(1)} Pi package`,
45
- `Run: ${command}\n\nThis operation can execute package code or mutate Pi settings/install roots. ${reloadWarning(operation)} Continue?`,
49
+ `Run: ${command}\n\nThis operation can execute package code or mutate Pi settings/install roots. ${reloadWarning(operation, { restartRequired })} Continue?`,
46
50
  );
47
51
  return approved
48
52
  ? { allowed: true, approved: true }
@@ -54,12 +58,16 @@ export async function installPackageWithPolicy(
54
58
  natives: Pick<Natives, "security" | "install" | "installService">,
55
59
  ctx: ApprovalContext,
56
60
  ) {
57
- const approval = await approvePackageOperation("install", `pi install ${source}`, natives, ctx);
61
+ const restartRequired = targetsPackedItself(source);
62
+ const approval = await approvePackageOperation("install", `pi install ${source}`, natives, ctx, restartRequired);
58
63
  if (!approval.allowed) {
59
64
  const output = approval.message ?? "install denied";
60
65
  return text(output, createMutationDetails("install", source, approval.reason ?? "denied", output));
61
66
  }
62
- let output = (await natives.install(source, approval.approved)) || `Installed ${source}. Reload with /reload to activate.`;
67
+ const activateNote = restartRequired
68
+ ? "Restart Pi (exit and relaunch) to activate -- Packed's own running code can't hot-reload itself."
69
+ : "Reload with /reload to activate.";
70
+ let output = (await natives.install(source, approval.approved)) || `Installed ${source}. ${activateNote}`;
63
71
  // Piggybacks on the same approval already granted for install -- both are
64
72
  // the same code-execution mutation tier, not a new consent surface. Silent
65
73
  // for the overwhelmingly common case (most Pi packages aren't daemons at
@@ -80,7 +88,8 @@ export async function installPackageWithPolicy(
80
88
  }
81
89
 
82
90
  export async function updatePackageWithPolicy(source: string, natives: Pick<Natives, "security" | "update">, ctx: ApprovalContext) {
83
- const approval = await approvePackageOperation("update", `pi update --extension ${source}`, natives, ctx);
91
+ const restartRequired = targetsPackedItself(source);
92
+ const approval = await approvePackageOperation("update", `pi update --extension ${source}`, natives, ctx, restartRequired);
84
93
  if (!approval.allowed) {
85
94
  const output = approval.message ?? "update denied";
86
95
  return text(output, createMutationDetails("update", source, approval.reason ?? "denied", output));
@@ -95,17 +104,24 @@ export async function updatePackageWithPolicy(source: string, natives: Pick<Nati
95
104
  return text(message, createMutationDetails("update", source, "succeeded", outcome.output || message, false));
96
105
  }
97
106
  const transition = outcome.previousVersion && outcome.currentVersion ? ` (${outcome.previousVersion} → ${outcome.currentVersion})` : "";
98
- const message = `${outcome.output || `Updated ${source}.`}${transition} Reload with /reload to activate.`;
107
+ const activateNote = restartRequired
108
+ ? "Restart Pi (exit and relaunch) to activate -- Packed's own running code can't hot-reload itself."
109
+ : "Reload with /reload to activate.";
110
+ const message = `${outcome.output || `Updated ${source}.`}${transition} ${activateNote}`;
99
111
  return text(message, createMutationDetails("update", source, "succeeded", outcome.output || message, true));
100
112
  }
101
113
 
102
114
  export async function removePackageWithPolicy(name: string, natives: Pick<Natives, "security" | "remove">, ctx: ApprovalContext) {
103
- const approval = await approvePackageOperation("remove", `pi remove npm:${name}`, natives, ctx);
115
+ const restartRequired = targetsPackedItself(name);
116
+ const approval = await approvePackageOperation("remove", `pi remove npm:${name}`, natives, ctx, restartRequired);
104
117
  if (!approval.allowed) {
105
118
  const output = approval.message ?? "remove denied";
106
119
  return text(output, createMutationDetails("remove", name, approval.reason ?? "denied", output));
107
120
  }
108
- const output = (await natives.remove(name, approval.approved)) || `Removed ${name}. Reload with /reload to deactivate.`;
121
+ const deactivateNote = restartRequired
122
+ ? "Restart Pi (exit and relaunch) to deactivate -- Packed's own running code can't hot-reload itself."
123
+ : "Reload with /reload to deactivate.";
124
+ const output = (await natives.remove(name, approval.approved)) || `Removed ${name}. ${deactivateNote}`;
109
125
  return text(output, createMutationDetails("remove", name, "succeeded", output));
110
126
  }
111
127
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danypops/pi-packed",
3
- "version": "0.21.14",
3
+ "version": "0.21.15",
4
4
  "description": "Pi package lifecycle, validation, daemon, tools, profiles, and TUI",
5
5
  "type": "module",
6
6
  "bin": {