pi-petrroll 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
@@ -11,7 +11,7 @@ Independent, focused extensions. No skills, shell wrappers, or replacement built
11
11
 
12
12
  ## Install
13
13
 
14
- Requires **Pi 0.85.1 or newer** (`@earendil-works/pi-coding-agent`); tested against 0.85.1. Change directory requires interactive mode; external cost recording works in all modes. Earlier Pi versions may not rebuild cwd-bound services when switching sessions.
14
+ Requires **Pi 0.85.1 or newer** (`@earendil-works/pi-coding-agent`); tested against 0.85.1 and 1.1.0. Change directory requires interactive mode; external cost recording works in all modes. Earlier Pi versions may not rebuild cwd-bound services when switching sessions.
15
15
 
16
16
  ```sh
17
17
  pi install npm:pi-petrroll
@@ -78,14 +78,14 @@ No quit, terminal `cd`, restart, or cross-directory session-copy confirmation. *
78
78
 
79
79
  Bare `/cd` shows the current directory and usage. Relative paths use Pi's current cwd. `~`, `~/…`, single-quoted paths, JSON double-quoted paths, and symlinks are supported. Paths are not shell commands: no variable expansion, globbing, `~username`, or `cd -`.
80
80
 
81
- Manual `/cd` switches without starting another model response; the agent tool resumes automatically. An existing destination is required. The extension does not create directories or worktrees itself.
81
+ Manual `/cd` switches without starting another model response; the agent tool resumes automatically **after session replacement has fully completed**. The tool intentionally ends the old run, but normally needs no manual `continue`. An existing destination is required. The extension does not create directories or worktrees itself.
82
82
 
83
83
  ### What changes
84
84
 
85
85
  - A new session ID and session file are created for the destination, using Pi's `SessionManager.forkFrom()`.
86
86
  - The original session is left in place. The full conversation tree is copied, and the currently selected branch is preserved.
87
87
  - Pi's native session replacement rebuilds built-in tools, the working-directory display, settings, extensions, skills, and project context such as `AGENTS.md` for the destination.
88
- - `pi -c` from the destination can continue the copied session. Copies use Pi's normal per-directory session storage (respecting `PI_CODING_AGENT_DIR`), not a source session's explicit `--session-dir` or `PI_CODING_AGENT_SESSION_DIR` override. If you use those overrides, resume the copy by its file path or remove the override.
88
+ - `pi -c` from the destination can continue the copied session. Copies stay in the source session's storage: if it uses a custom session directory (`--session-dir`, `PI_CODING_AGENT_SESSION_DIR`, or the `sessionDir` setting), the copy is written there too, so `pi --session <id>` and `/resume` find it under the same override. Otherwise copies use Pi's normal per-directory storage for the destination (respecting `PI_CODING_AGENT_DIR`).
89
89
  - No `process.chdir()` trick, built-in tool overrides, or Pi internals are patched. Your parent terminal's working directory does **not** change.
90
90
 
91
91
  ### Safety and limitations
@@ -98,6 +98,7 @@ Manual `/cd` switches without starting another model response; the agent tool re
98
98
  - The agent should call `change_directory` **alone**, after worktree creation has completed. The tool requests early termination and the command waits for idle so completed tool results are included in the copy. If mixed with other tools, those calls still run in the **old** directory; the switch waits until the run settles.
99
99
  - Explicitly aborting a pending tool handoff prevents the switch. Invalid paths and concurrent requests are rejected.
100
100
  - A cancelled session switch removes the unused copy and keeps the original session. If Pi fails while rebuilding the replacement runtime, its normal fatal-error handling applies; the original and any created copy remain available for recovery.
101
+ - If submitting the automatic continuation fails, the extension warns without treating the successful switch as a failure. Model errors and user aborts still follow Pi's normal behavior; the extension does not retry aborted runs. Send `continue` in the destination if needed.
101
102
  - The copied history can contain references to the old project. A visible context message records the change, and the new system prompt contains the destination's instructions.
102
103
  - Switching reloads project extensions according to Pi's trust policy. Install only code you trust.
103
104
 
@@ -130,7 +131,7 @@ npm run check
130
131
  npm test
131
132
  ```
132
133
 
133
- Tests cover cost validation, native cost accumulation, persistence/resume, compaction, path handling, copy/branch preservation, cancellation, aborts, concurrency, and a real Pi runtime handoff with a fake model stream. The runtime test verifies that tool results are copied, `read` and `bash` use the new cwd, destination `AGENTS.md` replaces source context, and the agent continues. Tests do not call a model API or use your credentials.
134
+ Tests cover cost validation, native cost accumulation, persistence/resume, compaction, path handling, copy/branch preservation, cancellation, aborts, concurrency, and a real Pi runtime handoff with a fake model stream. The runtime test verifies that tool results are copied, `read` and `bash` use the new cwd, destination `AGENTS.md` replaces source context, and the agent continues only after the host finishes switching. Unit tests also cover continuation-submission failures without rolling back the successful switch. Tests do not call a model API or use your credentials.
134
135
 
135
136
  ```text
136
137
  extensions/
@@ -39,6 +39,20 @@ function requireSavedSession(ctx: ExtensionContext): string {
39
39
  return file;
40
40
  }
41
41
 
42
+ /**
43
+ * Where the copied session should live. A custom session dir (--session-dir,
44
+ * PI_CODING_AGENT_SESSION_DIR, or the sessionDir setting) is shared across
45
+ * projects and must be kept; otherwise resume/listing in that dir cannot find
46
+ * the copy. The default dir is per-cwd, so the copy gets the target's default.
47
+ */
48
+ export function targetSessionDir(sessionManager: ExtensionContext["sessionManager"]): string | undefined {
49
+ // The live object is a full SessionManager; ReadonlySessionManager just does
50
+ // not expose this predicate in its type.
51
+ const usesDefault = (sessionManager as { usesDefaultSessionDir?: () => boolean }).usesDefaultSessionDir;
52
+ if (typeof usesDefault !== "function" || usesDefault.call(sessionManager)) return undefined;
53
+ return sessionManager.getSessionDir();
54
+ }
55
+
42
56
  export default function cdExtension(pi: ExtensionAPI) {
43
57
  let busy = false;
44
58
  let toolRequest: { argument: string; signal: AbortSignal | undefined } | undefined;
@@ -66,7 +80,7 @@ export default function cdExtension(pi: ExtensionAPI) {
66
80
  return;
67
81
  }
68
82
 
69
- const copy = SessionManager.forkFrom(source, target);
83
+ const copy = SessionManager.forkFrom(source, target, targetSessionDir(ctx.sessionManager));
70
84
  // forkFrom copies the whole tree but opens its last entry. Preserve the
71
85
  // actual selected leaf, including when the user navigated with /tree.
72
86
  const leaf = ctx.sessionManager.getLeafId();
@@ -80,15 +94,27 @@ export default function cdExtension(pi: ExtensionAPI) {
80
94
  { from: ctx.cwd, to: target, parentSession: source },
81
95
  );
82
96
  const destination = copy.getSessionFile()!;
83
- // Do not use captured pi/ctx in withSession: the old runtime is invalid.
97
+ let resume: (() => Promise<void>) | undefined;
84
98
  const result = await ctx.switchSession(destination, {
85
99
  withSession: async (fresh) => {
86
100
  fresh.ui.notify(`Changed directory to ${fresh.cwd}`, "info");
87
101
  if (request) {
88
- await fresh.sendUserMessage(
89
- "The requested directory change has completed. Continue the outstanding task in this directory. " +
90
- "If no work remains, briefly confirm the change.",
91
- );
102
+ // Capture only the fresh context, not the invalidated pi/ctx.
103
+ resume = async () => {
104
+ try {
105
+ await fresh.sendUserMessage(
106
+ "The requested directory change has completed. Continue the outstanding task in this directory. " +
107
+ "If no work remains, briefly confirm the change.",
108
+ { deliverAs: "followUp" },
109
+ );
110
+ } catch (error) {
111
+ fresh.ui.notify(
112
+ `Directory changed, but automatic continuation failed: ${error instanceof Error ? error.message : String(error)}. ` +
113
+ 'Send "continue" to resume.',
114
+ "warning",
115
+ );
116
+ }
117
+ };
92
118
  }
93
119
  },
94
120
  });
@@ -100,7 +126,12 @@ export default function cdExtension(pi: ExtensionAPI) {
100
126
  display: true,
101
127
  });
102
128
  ctx.ui.notify("Directory change cancelled.", "warning");
129
+ return;
103
130
  }
131
+ // sendUserMessage awaits the entire agent run. Do not run it inside
132
+ // withSession: the host must finish switching first, and prompt errors
133
+ // must not be treated as fatal failures to replace the runtime.
134
+ await resume?.();
104
135
  } finally {
105
136
  // Local state only; session-bound pi/ctx may now be stale.
106
137
  busy = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-petrroll",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Small, focused Pi extensions by petrroll: switch projects and track external costs in your session.",
5
5
  "type": "module",
6
6
  "license": "MIT",