@narumitw/pi-starship 0.34.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
@@ -8,7 +8,7 @@ A native Pi footer configured with Starship-style TOML. It parses and renders fo
8
8
 
9
9
  ## ✨ Features
10
10
 
11
- - Automatically creates a readable Tokyo Night configuration on first session start.
11
+ - Uses a readable built-in Tokyo Night configuration until the user explicitly saves settings.
12
12
  - Starship-style root/module formats, conditional groups, `$all`, styles, and palettes.
13
13
  - Pi modules for model, thinking, activity, context, tokens, cost, turn, and extension statuses.
14
14
  - Cached Git branch, commit, operation state, line metrics, detailed status, and linked-worktree identity.
@@ -40,7 +40,7 @@ The only configuration source is:
40
40
  <getAgentDir()>/pi-starship.toml
41
41
  ```
42
42
 
43
- On the first session start, the extension atomically creates this file from its readable Tokyo Night default. It never overwrites an existing document, including a malformed one. If initialization fails, the built-in configuration remains active and the failure is reported as a warning.
43
+ When this file is absent, the extension uses its readable Tokyo Night default without creating the file or parent directory. The first successful settings save creates it atomically. Existing malformed documents are never overwritten.
44
44
 
45
45
  The extension does **not** read project overrides, `pi-statusline.json`, `PI_STATUSLINE_PRESET`, or `~/.config/starship.toml`, and does not migrate statusline settings.
46
46
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-starship",
3
- "version": "0.34.0",
3
+ "version": "0.35.0",
4
4
  "description": "Native Starship-style TOML statusline for the Pi coding agent.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/commands.ts CHANGED
@@ -11,6 +11,7 @@ import {
11
11
  atomicSaveConfigDocument,
12
12
  BUILT_IN_EXAMPLE,
13
13
  type LoadedStarshipConfig,
14
+ removeConfigDocumentIfMatches,
14
15
  validateConfigDocument,
15
16
  } from "./config.js";
16
17
 
@@ -264,7 +265,7 @@ async function reviewAndApply(
264
265
  try {
265
266
  options.apply(saved, ctx);
266
267
  } catch (error) {
267
- const rollbackError = restorePreviousConfiguration(ctx, options, previous);
268
+ const rollbackError = restorePreviousConfiguration(ctx, options, previous, saved);
268
269
  ctx.ui.notify(
269
270
  rollbackError
270
271
  ? `Footer settings could not be applied: ${safeText(formatError(error))}. Restoring the previous configuration also failed: ${safeText(formatError(rollbackError))}.`
@@ -292,12 +293,16 @@ function restorePreviousConfiguration(
292
293
  ctx: ExtensionCommandContext,
293
294
  options: StarshipCommandOptions,
294
295
  previous: LoadedStarshipConfig,
296
+ saved: LoadedStarshipConfig,
295
297
  ): unknown {
296
298
  try {
297
- if (previous.rawDocument === undefined) {
298
- throw new Error("The previous settings document is unavailable");
299
+ if (saved.rawDocument === undefined || saved.fileIdentity === undefined) {
300
+ throw new Error("The saved settings document identity is unavailable");
301
+ }
302
+ removeConfigDocumentIfMatches(options.settingsPath, saved.rawDocument, saved.fileIdentity);
303
+ if (previous.rawDocument !== undefined) {
304
+ (options.restore ?? atomicRestoreConfigDocument)(options.settingsPath, previous.rawDocument);
299
305
  }
300
- (options.restore ?? atomicRestoreConfigDocument)(options.settingsPath, previous.rawDocument);
301
306
  options.apply(previous, ctx);
302
307
  return undefined;
303
308
  } catch (error) {
package/src/config.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import {
3
3
  existsSync,
4
- linkSync,
4
+ lstatSync,
5
5
  mkdirSync,
6
6
  readFileSync,
7
7
  renameSync,
@@ -69,6 +69,7 @@ export interface LoadedStarshipConfig {
69
69
  source: "built-in" | "user";
70
70
  settingsPath: string;
71
71
  rawDocument?: string;
72
+ fileIdentity?: { dev: number; ino: number };
72
73
  diagnostics: ConfigDiagnostic[];
73
74
  }
74
75
 
@@ -157,7 +158,7 @@ export function loadStarshipConfig(settingsPath: string): LoadedStarshipConfig {
157
158
  try {
158
159
  rawDocument = readFileSync(settingsPath, "utf8");
159
160
  } catch (error) {
160
- if (!existsSync(settingsPath)) {
161
+ if ((error as NodeJS.ErrnoException).code === "ENOENT" && !existsSync(settingsPath)) {
161
162
  return {
162
163
  config: cloneBuiltInConfig(),
163
164
  source: "built-in",
@@ -194,48 +195,6 @@ export function loadStarshipConfig(settingsPath: string): LoadedStarshipConfig {
194
195
  };
195
196
  }
196
197
 
197
- interface InitialFileSystem {
198
- mkdirSync: typeof mkdirSync;
199
- writeFileSync: typeof writeFileSync;
200
- linkSync: typeof linkSync;
201
- rmSync: typeof rmSync;
202
- }
203
-
204
- export function loadOrCreateStarshipConfig(
205
- settingsPath: string,
206
- overrides: Partial<InitialFileSystem> = {},
207
- ): LoadedStarshipConfig {
208
- const loaded = loadStarshipConfig(settingsPath);
209
- if (loaded.source === "user" || loaded.diagnostics.length > 0) return loaded;
210
-
211
- const fs = { mkdirSync, writeFileSync, linkSync, rmSync, ...overrides };
212
- const tempPath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.tmp`);
213
- try {
214
- fs.mkdirSync(dirname(settingsPath), { recursive: true });
215
- fs.writeFileSync(tempPath, BUILT_IN_EXAMPLE, { encoding: "utf8", flag: "wx" });
216
- try {
217
- fs.linkSync(tempPath, settingsPath);
218
- } catch (error) {
219
- if (isAlreadyExistsError(error)) return loadStarshipConfig(settingsPath);
220
- throw error;
221
- }
222
- return loadStarshipConfig(settingsPath);
223
- } catch (error) {
224
- return {
225
- ...loaded,
226
- diagnostics: [
227
- diagnostic("warning", "", `Unable to create default settings: ${formatError(error)}`),
228
- ],
229
- };
230
- } finally {
231
- try {
232
- fs.rmSync(tempPath, { force: true });
233
- } catch {
234
- // Best-effort cleanup must not replace the initialization result.
235
- }
236
- }
237
- }
238
-
239
198
  export function normalizeConfig(value: unknown): {
240
199
  config: StarshipConfig;
241
200
  diagnostics: ConfigDiagnostic[];
@@ -472,8 +431,10 @@ export function atomicSaveConfigDocument(
472
431
  overrides: Partial<AtomicFileSystem> = {},
473
432
  ): LoadedStarshipConfig {
474
433
  const validated = validateConfigDocument(settingsPath, rawDocument);
475
- atomicWriteConfigDocument(settingsPath, rawDocument, overrides);
476
- return validated;
434
+ return {
435
+ ...validated,
436
+ fileIdentity: atomicWriteConfigDocument(settingsPath, rawDocument, overrides),
437
+ };
477
438
  }
478
439
 
479
440
  export function atomicRestoreConfigDocument(
@@ -484,17 +445,58 @@ export function atomicRestoreConfigDocument(
484
445
  atomicWriteConfigDocument(settingsPath, rawDocument, overrides);
485
446
  }
486
447
 
448
+ export function removeConfigDocumentIfMatches(
449
+ settingsPath: string,
450
+ expectedRawDocument: string,
451
+ expectedIdentity: { dev: number; ino: number },
452
+ overrides: Pick<Partial<AtomicFileSystem>, "rmSync"> = {},
453
+ ) {
454
+ const quarantinePath = join(
455
+ dirname(settingsPath),
456
+ `.${CONFIG_FILE_NAME}.${randomUUID()}.rollback`,
457
+ );
458
+ const before = lstatSync(settingsPath);
459
+ if (before.dev !== expectedIdentity.dev || before.ino !== expectedIdentity.ino) {
460
+ throw new Error("Starship settings changed concurrently; the newer file was preserved");
461
+ }
462
+ renameSync(settingsPath, quarantinePath);
463
+ const quarantined = lstatSync(quarantinePath);
464
+ const quarantinedSavedFile =
465
+ quarantined.isFile() &&
466
+ !quarantined.isSymbolicLink() &&
467
+ quarantined.dev === expectedIdentity.dev &&
468
+ quarantined.ino === expectedIdentity.ino;
469
+ if (quarantinedSavedFile && readFileSync(quarantinePath, "utf8") === expectedRawDocument) {
470
+ (overrides.rmSync ?? rmSync)(quarantinePath);
471
+ return;
472
+ }
473
+ if (quarantinedSavedFile && !pathEntryExists(settingsPath)) {
474
+ try {
475
+ renameSync(quarantinePath, settingsPath);
476
+ } catch {
477
+ // Keep the quarantine for recovery when its atomic restoration fails.
478
+ }
479
+ }
480
+ throw new Error("Starship settings changed concurrently; the newer file was preserved");
481
+ }
482
+
487
483
  function atomicWriteConfigDocument(
488
484
  settingsPath: string,
489
485
  rawDocument: string,
490
486
  overrides: Partial<AtomicFileSystem>,
491
- ) {
487
+ ): { dev: number; ino: number } {
492
488
  const fs = { mkdirSync, writeFileSync, renameSync, rmSync, ...overrides };
489
+ const replaceExisting = pathEntryExists(settingsPath);
493
490
  fs.mkdirSync(dirname(settingsPath), { recursive: true });
494
491
  const tempPath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.tmp`);
495
492
  try {
496
493
  fs.writeFileSync(tempPath, rawDocument, { encoding: "utf8", flag: "wx" });
494
+ const info = lstatSync(tempPath);
495
+ if (!replaceExisting && pathEntryExists(settingsPath)) {
496
+ throw new Error(`${CONFIG_FILE_NAME} was created concurrently; reopen settings and retry.`);
497
+ }
497
498
  fs.renameSync(tempPath, settingsPath);
499
+ return { dev: info.dev, ino: info.ino };
498
500
  } finally {
499
501
  try {
500
502
  fs.rmSync(tempPath, { force: true });
@@ -648,8 +650,14 @@ function diagnostic(
648
650
  return { severity, path, message };
649
651
  }
650
652
 
651
- function isAlreadyExistsError(error: unknown): boolean {
652
- return error instanceof Error && "code" in error && error.code === "EEXIST";
653
+ function pathEntryExists(path: string): boolean {
654
+ try {
655
+ lstatSync(path);
656
+ return true;
657
+ } catch (error) {
658
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
659
+ throw error;
660
+ }
653
661
  }
654
662
 
655
663
  function formatError(error: unknown): string {
@@ -9,7 +9,6 @@ import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
9
9
  import { registerStarshipCommand } from "./commands.js";
10
10
  import {
11
11
  type LoadedStarshipConfig,
12
- loadOrCreateStarshipConfig,
13
12
  loadStarshipConfig,
14
13
  type StarshipConfig,
15
14
  settingsFilePath,
@@ -237,7 +236,7 @@ export default function piStarship(pi: ExtensionAPI) {
237
236
  });
238
237
 
239
238
  pi.on("session_start", (_event, ctx) => {
240
- loaded = loadOrCreateStarshipConfig(configPath);
239
+ loaded = loadStarshipConfig(configPath);
241
240
  if (loaded.diagnostics.length > 0 && (ctx.mode === "tui" || ctx.hasUI)) {
242
241
  ctx.ui.notify(formatDiagnostics(loaded), "warning");
243
242
  }