@narumitw/pi-worktree 0.31.0 → 0.35.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
@@ -95,9 +95,9 @@ For a default Pi installation this is typically `~/.pi/agent/pi-worktree.json`.
95
95
 
96
96
  `worktreeRoot` accepts `~`, a home-prefixed path such as `~/worktrees`, or a native-platform absolute path. It does not expand `$VAR`, `%VAR%`, or other shell syntax. Empty, relative, NUL-containing, non-string, and invalid paths are rejected. There is no project override or extension-specific environment variable.
97
97
 
98
- A missing `worktreeRoot` uses `~/.worktrees`. Submitting a blank value in the interactive action removes the override. Unknown JSON fields survive interactive saves and resets. Settings reload on every `session_start`, including `/reload` and workspace replacement; a successful interactive save applies immediately to the next Add flow.
98
+ A missing `worktreeRoot` uses `~/.worktrees`; the settings file is created only by a successful interactive change. Submitting a blank value in the interactive action removes the override. Within one Pi process, queued saves run in invocation order, reread the latest valid document immediately before merging `worktreeRoot`, and preserve concurrent unknown-field edits. Settings reload on every `session_start`, including `/reload` and workspace replacement; a successful interactive save applies immediately to the next Add flow.
99
99
 
100
- Malformed or invalid settings are warned about but never overwritten. An initial failure uses `~/.worktrees`; a later failure retains the last valid effective root. Interactive configuration remains blocked until the invalid file is fixed manually.
100
+ Malformed or invalid settings are warned about but never overwritten, including an invalid edit made while a settings action is open. An initial failure uses `~/.worktrees`; a later failure retains the last valid effective root. Interactive configuration remains blocked until the invalid file is fixed manually. Failed publication leaves the prior file and effective runtime root unchanged, and the save queue remains usable after rejection.
101
101
 
102
102
  ## 🔀 Pi workspace switching
103
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-worktree",
3
- "version": "0.31.0",
3
+ "version": "0.35.0",
4
4
  "description": "Pi extension for safe interactive Git worktree management and workspace switching.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/settings.ts CHANGED
@@ -35,6 +35,7 @@ export interface WorktreeSettingsRuntime {
35
35
  getPath(): string;
36
36
  reload(): Promise<Readonly<WorktreeSettingsState>>;
37
37
  save(configuredRoot: string | undefined): Promise<Readonly<WorktreeSettingsState>>;
38
+ flush?(): Promise<void>;
38
39
  }
39
40
 
40
41
  interface RuntimeOptions {
@@ -187,7 +188,6 @@ export function createWorktreeSettingsRuntime(
187
188
  typeof options.path === "function" ? options.path() : (options.path ?? settingsFilePath());
188
189
  return resolvedPath;
189
190
  };
190
- let document: Record<string, unknown> = {};
191
191
  let operationQueue = Promise.resolve();
192
192
  const enqueue = <T>(operation: () => Promise<T>): Promise<T> => {
193
193
  const result = operationQueue.then(operation, operation);
@@ -206,6 +206,9 @@ export function createWorktreeSettingsRuntime(
206
206
  return {
207
207
  get: () => Object.freeze({ ...state }),
208
208
  getPath,
209
+ async flush() {
210
+ await operationQueue;
211
+ },
209
212
  reload() {
210
213
  return enqueue(async () => {
211
214
  const loaded = await loadWorktreeSettings(getPath(), home, platform);
@@ -213,7 +216,6 @@ export function createWorktreeSettingsRuntime(
213
216
  state = { ...state, warning: loaded.warning, canSave: false };
214
217
  return Object.freeze({ ...state });
215
218
  }
216
- document = loaded.document ?? {};
217
219
  state = stateFromLoaded(loaded);
218
220
  return Object.freeze({ ...state });
219
221
  });
@@ -227,13 +229,17 @@ export function createWorktreeSettingsRuntime(
227
229
  configuredRoot === undefined
228
230
  ? defaultWorktreeRoot(home, platform)
229
231
  : resolveWorktreeRoot(configuredRoot, home, platform);
230
- const nextDocument = await saveWorktreeSettings(
231
- document,
232
+ const latest = await loadWorktreeSettings(getPath(), home, platform);
233
+ if (latest.kind === "invalid") {
234
+ state = { ...state, warning: latest.warning, canSave: false };
235
+ throw new Error(`Fix the pi-worktree settings file at ${getPath()} before changing it.`);
236
+ }
237
+ await saveWorktreeSettings(
238
+ latest.document ?? {},
232
239
  configuredRoot,
233
240
  getPath(),
234
241
  options.operations,
235
242
  );
236
- document = nextDocument;
237
243
  state = {
238
244
  effectiveRoot,
239
245
  source: configuredRoot === undefined ? "default" : "user",
package/src/worktree.ts CHANGED
@@ -15,15 +15,17 @@ export default function worktreeExtension(
15
15
  options: WorktreeExtensionOptions = {},
16
16
  ): void {
17
17
  const settings = options.settings ?? createWorktreeSettingsRuntime({ path: settingsFilePath });
18
+ let sessionGeneration = 0;
18
19
  registerWorktreeCommand(pi, settings);
19
20
 
20
21
  pi.on("session_start", async (_event, ctx) => {
22
+ const generation = ++sessionGeneration;
21
23
  const loaded = await settings.reload();
22
- if (!loaded.warning || !ctx.hasUI) return;
23
- try {
24
- ctx.ui.notify(loaded.warning, "warning");
25
- } catch {
26
- // A replacement session may invalidate its predecessor context during async startup.
27
- }
24
+ if (generation !== sessionGeneration || !loaded.warning || !ctx.hasUI) return;
25
+ ctx.ui.notify(loaded.warning, "warning");
26
+ });
27
+ pi.on("session_shutdown", async () => {
28
+ sessionGeneration += 1;
29
+ await settings.flush?.();
28
30
  });
29
31
  }