@bismawy/pi-arnative 0.6.0 → 0.6.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.6.1] - 2026-10-19
4
+
5
+ ### Fixed
6
+ - **A preset choice survives a second pi session.** `saveChoice` merged into a memo read once per process and wrote the whole file back, so a session that had already loaded the file dropped the keys another session had written: two concurrent saves left only the last one. Every save now re-reads the file, merges its one key, and replaces the file atomically (temp file + `rename`). The self-check writes the file behind the module and asserts the other key survives (`lib/preset-store.ts`).
7
+ - **The usage cache is replaced atomically.** `saveDiskCache` wrote in place, so a second pi session could read a truncated JSON, discard the cache and rescan every session file. It now writes a temp file and renames it, and `Object.fromEntries` replaces the manual copy loop (`lib/usage-store.ts`).
8
+
9
+ ### Changed
10
+ - **`ROW_DIVIDER` has one definition.** The divider between a picker row label and its note was declared twice, once in the picker and once in the animations module. It now lives in `lib/format.ts` beside `markedLabels`, and both modules import it (`lib/format.ts`, `extensions/arnative.ts`, `extensions/animations.ts`).
11
+
3
12
  ## [0.6.0] - 2026-10-17
4
13
 
5
14
  ### Fixed
@@ -25,10 +25,7 @@ import {
25
25
  type SpinnerPreset,
26
26
  } from "../lib/spinner.ts";
27
27
  import { assert, isMain } from "../lib/check.ts";
28
-
29
- /** The row divider, matching the one `arnative.ts` draws (`Label · note`). Kept local so
30
- * this module does not reach into the picker just for a separator string. */
31
- const ROW_DIVIDER = " · ";
28
+ import { ROW_DIVIDER } from "../lib/format.ts";
32
29
 
33
30
  /** The arnative editor instance, published by `footer.ts` so the live spinner is reachable. */
34
31
  export const EDITOR_KEY = Symbol.for("pi-arnative.editor");
@@ -10,7 +10,7 @@ import { matchesKey, truncateToWidth, visibleWidth, type AutocompleteItem, type
10
10
  import { softSecondaryOf, stripAnsi, thinkingColorOf } from "../lib/ansi.ts";
11
11
  import { renderScreen, screenTitle, type ScreenText } from "../lib/box.ts";
12
12
  import { assert, isMain } from "../lib/check.ts";
13
- import { markedLabels } from "../lib/format.ts";
13
+ import { markedLabels, ROW_DIVIDER } from "../lib/format.ts";
14
14
  import { LEGEND_LIVE, LEGEND_MOVE, matchesBack } from "../lib/shortcuts.ts";
15
15
  import { SPINNER_PRESETS, type SpinnerPreset } from "../lib/spinner.ts";
16
16
  import {
@@ -42,9 +42,6 @@ export interface ListRow {
42
42
  note?: string;
43
43
  }
44
44
 
45
- /** Divider between row label and note, padded to align across rows. */
46
- export const ROW_DIVIDER = " · ";
47
-
48
45
  /** Menu rows: `Label · description` (same rhythm as /jev-eye's menu). */
49
46
  export function menuRows(menus: Pick<ArnativeMenu, "label" | "description">[]): ListRow[] {
50
47
  return menus.map((m) => ({ label: m.label, note: m.description }));
package/lib/format.ts CHANGED
@@ -23,6 +23,9 @@ export function modelDisplayParts(model?: { id?: string; name?: string; provider
23
23
  return { name, provider };
24
24
  }
25
25
 
26
+ // Divider between a picker row label and its note (Label · note), shared by every /arnative list.
27
+ export const ROW_DIVIDER = " · ";
28
+
26
29
  // Picker rows for every /arnative list (themes, header & footer presets):
27
30
  // `● name` marks the active entry, everything else gets a two-space indent.
28
31
  export function markedLabels(names: readonly string[], current: string | undefined): string[] {
@@ -6,10 +6,10 @@
6
6
  * editing it directly would race the manager and could drop concurrent changes. The choices here
7
7
  * belong to this package, not to pi.
8
8
  *
9
- * The file is read once per process and merged on write, so two extensions writing different
10
- * keys cannot clobber each other.
9
+ * Reads are memoised per process; every write re-reads the file and replaces it atomically
10
+ * (temp file + rename), so concurrent pi sessions that write different keys keep both.
11
11
  */
12
- import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
12
+ import { mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
13
13
  import { tmpdir } from "node:os";
14
14
  import { dirname, join } from "node:path";
15
15
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
@@ -23,20 +23,18 @@ const configPath = (): string => join(getAgentDir(), "arnative-config.json");
23
23
 
24
24
  let memo: Record<string, unknown> | null = null;
25
25
 
26
- function readConfig(): Record<string, unknown> {
27
- if (memo) return memo;
28
- memo = {};
26
+ function readDisk(): Record<string, unknown> {
29
27
  try {
30
- if (existsSync(configPath())) {
31
- const raw = JSON.parse(readFileSync(configPath(), "utf8")) as unknown;
32
- if (raw && typeof raw === "object" && !Array.isArray(raw)) memo = raw as Record<string, unknown>;
33
- }
28
+ const raw = JSON.parse(readFileSync(configPath(), "utf8")) as unknown;
29
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw as Record<string, unknown>;
34
30
  } catch {
35
- // unreadable/corrupt: fall back to defaults rather than crash the header
31
+ // absent, unreadable or corrupt: fall back to defaults rather than crash the header
36
32
  }
37
- return memo;
33
+ return {};
38
34
  }
39
35
 
36
+ const readConfig = (): Record<string, unknown> => (memo ??= readDisk());
37
+
40
38
  /** The stored value for `key`, or undefined when absent or not one of `allowed`. */
41
39
  export function loadChoice<T extends string>(key: string, allowed: readonly T[]): T | undefined {
42
40
  const value = readConfig()[key];
@@ -45,13 +43,15 @@ export function loadChoice<T extends string>(key: string, allowed: readonly T[])
45
43
 
46
44
  /** Store a choice. Best effort: an unwritable agent dir must not break the TUI. */
47
45
  export function saveChoice(key: string, value: string): void {
48
- const current = readConfig();
49
- if (current[key] === value) return;
50
- current[key] = value;
46
+ if (readConfig()[key] === value) return;
47
+ // Re-read: another pi session may have written its own key since this one loaded.
48
+ memo = { ...readDisk(), [key]: value };
51
49
  try {
52
- const dir = dirname(configPath());
53
- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
54
- writeFileSync(configPath(), `${JSON.stringify(current, null, 2)}\n`, "utf8");
50
+ const path = configPath();
51
+ const tmp = `${path}.${process.pid}.tmp`;
52
+ mkdirSync(dirname(path), { recursive: true });
53
+ writeFileSync(tmp, `${JSON.stringify(memo, null, 2)}\n`, "utf8");
54
+ renameSync(tmp, path);
55
55
  } catch {
56
56
  // ignore: the choice still applies for this session
57
57
  }
@@ -74,6 +74,12 @@ if (isMain(import.meta.url)) {
74
74
  assert(loadChoice(HEADER_PRESET_KEY, ["a", "b"] as const) === "b", "a saved choice reads back");
75
75
  assert(loadChoice(FOOTER_PRESET_KEY, ["a", "b"] as const) === "a", "a second key is stored without clobbering the first");
76
76
 
77
+ // Another pi session writes its own key after this one loaded; a save must keep it.
78
+ writeFileSync(join(fixture, "arnative-config.json"), JSON.stringify({ headerPreset: "b", footerPreset: "a", other: "x" }), "utf8");
79
+ saveChoice(FOOTER_PRESET_KEY, "b");
80
+ resetPresetCache();
81
+ assert(loadChoice("other", ["x"] as const) === "x", "a key written by another process survives a save");
82
+
77
83
  // The point of the file: the choice outlives the process (drop the in-memory copy).
78
84
  resetPresetCache();
79
85
  assert(loadChoice(HEADER_PRESET_KEY, ["a", "b"] as const) === "b", "the choice survives a restart (read from disk)");
@@ -89,4 +95,4 @@ if (isMain(import.meta.url)) {
89
95
 
90
96
  rmSync(fixture, { recursive: true, force: true });
91
97
  console.log("preset-store.ts self-check OK");
92
- }
98
+ }
@@ -6,7 +6,7 @@
6
6
  * - No double counting: the active session is already in its own file, so it is
7
7
  * NOT re-added from sessionManager (that inflated the ↑↓ totals).
8
8
  */
9
- import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, statSync, writeFileSync } from "node:fs";
9
+ import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, statSync, writeFileSync } from "node:fs";
10
10
  import { dirname, join } from "node:path";
11
11
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
12
12
 
@@ -231,13 +231,12 @@ function loadDiskCache(): Map<string, FileCacheItem> {
231
231
 
232
232
  function saveDiskCache(map: Map<string, FileCacheItem>): void {
233
233
  try {
234
- const obj: Record<string, FileCacheItem> = {};
235
- for (const [k, v] of map.entries()) {
236
- obj[k] = v;
237
- }
238
- const dir = dirname(cacheFilePath());
239
- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
240
- writeFileSync(cacheFilePath(), JSON.stringify(obj), "utf8");
234
+ // Temp file + rename: a concurrent pi session never reads a half-written cache.
235
+ const path = cacheFilePath();
236
+ const tmp = `${path}.${process.pid}.tmp`;
237
+ mkdirSync(dirname(path), { recursive: true });
238
+ writeFileSync(tmp, JSON.stringify(Object.fromEntries(map)), "utf8");
239
+ renameSync(tmp, path);
241
240
  } catch {
242
241
  // ignore
243
242
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bismawy/pi-arnative",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Refined aesthetics. Cohesive tools. Built for Pi. Warm themes, rounded tool boxes, tabbed header, compact footer and a /usage dashboard.",
5
5
  "keywords": [
6
6
  "pi-package",