pi-charset 0.1.8 → 0.1.9

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.
@@ -84,11 +84,35 @@ function defaultBackupRoot(home: string, stateOverride?: string): string {
84
84
  return path.join(process.env.XDG_STATE_HOME || path.join(home, ".local", "state"), "pi-charset", "setup-backups");
85
85
  }
86
86
 
87
+ /** Resolve a CLI executable from PATH without spawning slow `--version` subprocesses (supports Windows .exe/.cmd/.bat/.ps1 shims). */
87
88
  function probeBinary(cmd: string): string | null {
88
- try {
89
- const r = spawnSync(cmd, ["--version"], { stdio: "ignore", windowsHide: true, timeout: 2500 });
90
- if (!r.error && r.status === 0) return cmd;
91
- } catch { /* not found */ }
89
+ const pathEnv = process.env.PATH || process.env.Path || "";
90
+ const dirs = pathEnv.split(path.delimiter).filter(Boolean);
91
+ if (process.platform === "win32") {
92
+ const exts = (process.env.PATHEXT || ".COM;.EXE;.BAT;.CMD")
93
+ .split(";")
94
+ .map((e) => e.trim())
95
+ .filter(Boolean);
96
+ if (!exts.some((e) => e.toUpperCase() === ".PS1")) exts.push(".PS1");
97
+ for (const dir of dirs) {
98
+ const cleanDir = dir.replace(/^"|"$/g, "");
99
+ if (!cleanDir) continue;
100
+ for (const ext of exts) {
101
+ const candidate = path.join(cleanDir, `${cmd}${ext.toLowerCase()}`);
102
+ try {
103
+ if (fs.statSync(candidate).isFile()) return candidate;
104
+ } catch { /* next */ }
105
+ }
106
+ }
107
+ return null;
108
+ }
109
+ for (const dir of dirs) {
110
+ const candidate = path.join(dir, cmd);
111
+ try {
112
+ fs.accessSync(candidate, fs.constants.X_OK);
113
+ if (fs.statSync(candidate).isFile()) return candidate;
114
+ } catch { /* next */ }
115
+ }
92
116
  return null;
93
117
  }
94
118
 
@@ -141,6 +165,7 @@ function isRunnableEntrypoint(command: string | undefined, args: readonly string
141
165
  interface JsonMcpRecord {
142
166
  command?: string;
143
167
  args?: string[];
168
+ defaultToolsApprovalMode?: string;
144
169
  }
145
170
 
146
171
  function readJsonMcpEntry(configPath: string): {
@@ -253,23 +278,66 @@ function findCodexSection(lines: readonly string[]): { start: number; end: numbe
253
278
  return { start, end };
254
279
  }
255
280
 
281
+ function parseTomlStringToken(raw: string): string | undefined {
282
+ const s = raw.trim();
283
+ if (s.startsWith("'") && s.endsWith("'") && s.length >= 2) {
284
+ // TOML literal string: backslashes are literal characters (used by Codex CLI on Windows paths).
285
+ return s.slice(1, -1);
286
+ }
287
+ if (s.startsWith('"') && s.endsWith('"') && s.length >= 2) {
288
+ try { return JSON.parse(s); } catch { return undefined; }
289
+ }
290
+ return undefined;
291
+ }
292
+
293
+ function parseTomlStringArray(raw: string): string[] | undefined {
294
+ const s = raw.trim();
295
+ if (!s.startsWith("[") || !s.endsWith("]")) return undefined;
296
+ const inner = s.slice(1, -1).trim();
297
+ if (!inner) return [];
298
+ const items: string[] = [];
299
+ let i = 0;
300
+ while (i < inner.length) {
301
+ while (i < inner.length && /[\s,]/.test(inner[i])) i++;
302
+ if (i >= inner.length) break;
303
+ const q = inner[i];
304
+ if (q === "'") {
305
+ const end = inner.indexOf("'", i + 1);
306
+ if (end === -1) return undefined;
307
+ items.push(inner.slice(i + 1, end));
308
+ i = end + 1;
309
+ } else if (q === '"') {
310
+ let j = i + 1;
311
+ while (j < inner.length) {
312
+ if (inner[j] === "\\") { j += 2; continue; }
313
+ if (inner[j] === '"') break;
314
+ j++;
315
+ }
316
+ if (j >= inner.length) return undefined;
317
+ const parsed = parseTomlStringToken(inner.slice(i, j + 1));
318
+ if (parsed === undefined) return undefined;
319
+ items.push(parsed);
320
+ i = j + 1;
321
+ } else {
322
+ return undefined;
323
+ }
324
+ }
325
+ return items;
326
+ }
327
+
256
328
  function parseCodexSectionEntry(sectionLines: readonly string[]): JsonMcpRecord | undefined {
257
329
  let command: string | undefined;
258
330
  let args: string[] | undefined;
331
+ let defaultToolsApprovalMode: string | undefined;
259
332
  for (const line of sectionLines) {
260
- const cm = /^\s*command\s*=\s*("(?:[^"\\]|\\.)*")/.exec(line);
261
- if (cm) {
262
- try { command = JSON.parse(cm[1]); } catch { /* ignore */ }
263
- }
333
+ const cm = /^\s*command\s*=\s*("(?:[^"\\]|\\.)*"|'[^']*')\s*(?:#.*)?$/.exec(line);
334
+ if (cm) command = parseTomlStringToken(cm[1]);
264
335
  const am = /^\s*args\s*=\s*(\[.*\])\s*(?:#.*)?$/.exec(line);
265
- if (am) {
266
- try {
267
- const parsed = JSON.parse(am[1]);
268
- if (Array.isArray(parsed) && parsed.every((x) => typeof x === "string")) args = parsed;
269
- } catch { /* ignore */ }
270
- }
336
+ if (am) args = parseTomlStringArray(am[1]);
337
+ const dm = /^\s*default_tools_approval_mode\s*=\s*("(?:[^"\\]|\\.)*"|'[^']*')\s*(?:#.*)?$/.exec(line);
338
+ if (dm) defaultToolsApprovalMode = parseTomlStringToken(dm[1]);
271
339
  }
272
- return command || args ? { command, args } : undefined;
340
+ return command || args ? { command, args, ...(defaultToolsApprovalMode ? { defaultToolsApprovalMode } : {}) } : undefined;
273
341
  }
274
342
 
275
343
  function readCodexTomlEntry(configPath: string): { exists: boolean; raw: string; entry?: JsonMcpRecord; sectionText?: string } {
@@ -282,19 +350,23 @@ function readCodexTomlEntry(configPath: string): { exists: boolean; raw: string;
282
350
  return { exists: true, raw, entry: parseCodexSectionEntry(sectionLines), sectionText: sectionLines.join("\n") };
283
351
  }
284
352
 
285
- function formatCodexSection(desired: { command: string; args: string[] }): string[] {
353
+ function formatCodexSection(desired: { command: string; args: string[]; defaultToolsApprovalMode?: string }): string[] {
286
354
  return [
287
355
  `[mcp_servers.pi-charset]`,
288
356
  `command = ${JSON.stringify(desired.command)}`,
289
357
  `args = ${JSON.stringify(desired.args)}`,
358
+ ...(desired.defaultToolsApprovalMode !== undefined
359
+ ? [`default_tools_approval_mode = ${JSON.stringify(desired.defaultToolsApprovalMode)}`]
360
+ : []),
290
361
  ];
291
362
  }
292
363
 
293
364
  function updateCodexTomlConfig(
294
365
  configPath: string,
295
366
  backupRoot: string,
296
- desired: { command: string; args: string[] } | null,
367
+ desired: { command: string; args: string[]; defaultToolsApprovalMode?: string } | null,
297
368
  dryRun: boolean,
369
+ isRollback = false,
298
370
  ): { action: SetupActionResult["action"]; detail: string } {
299
371
  const cur = readCodexTomlEntry(configPath);
300
372
  const eol = cur.raw.includes("\r\n") ? "\r\n" : "\n";
@@ -312,7 +384,18 @@ function updateCodexTomlConfig(
312
384
  return { action: "uninstalled", detail: `Removed [mcp_servers.pi-charset] from ${configPath}` };
313
385
  }
314
386
 
315
- if (cur.entry && cur.entry.command === desired.command && sameStringArray(cur.entry.args, desired.args)) {
387
+ // Default to `approve` so local encoding tools work in Codex `workspace-write` / `codex exec` without spurious blocks,
388
+ // while preserving any explicit user-chosen `default_tools_approval_mode` (and honoring exact backup values on rollback).
389
+ const targetApprovalMode = isRollback
390
+ ? desired.defaultToolsApprovalMode
391
+ : desired.defaultToolsApprovalMode ?? cur.entry?.defaultToolsApprovalMode ?? "approve";
392
+
393
+ if (
394
+ cur.entry &&
395
+ cur.entry.command === desired.command &&
396
+ sameStringArray(cur.entry.args, desired.args) &&
397
+ cur.entry.defaultToolsApprovalMode === targetApprovalMode
398
+ ) {
316
399
  return { action: "unchanged", detail: `Already registered with current entrypoint in ${configPath}` };
317
400
  }
318
401
  const action = span ? "updated" : "registered";
@@ -320,7 +403,7 @@ function updateCodexTomlConfig(
320
403
  return { action, detail: `[dry-run] Would ${action === "updated" ? "update" : "add"} [mcp_servers.pi-charset] in ${configPath}` };
321
404
  }
322
405
  saveBackup(backupRoot, "codex", cur.entry ?? null);
323
- const block = formatCodexSection(desired);
406
+ const block = formatCodexSection({ ...desired, defaultToolsApprovalMode: targetApprovalMode });
324
407
  let nextLines: string[];
325
408
  if (span) {
326
409
  nextLines = [...lines.slice(0, span.start), ...block, "", ...lines.slice(span.end)];
@@ -451,14 +534,19 @@ export function inspectClients(opts: { homeDir?: string; skipCliProbe?: boolean
451
534
  continue;
452
535
  }
453
536
  const runnable = isRunnableEntrypoint(cur.entry.command, cur.entry.args);
537
+ const missingApprovalMode = runnable && !cur.entry.defaultToolsApprovalMode;
454
538
  out.push({
455
539
  id,
456
540
  label: info.label,
457
541
  detected: true,
458
542
  cliPath,
459
543
  configPath: info.configPath,
460
- status: runnable ? "registered" : "degraded",
461
- detail: runnable ? "Registered in ~/.codex/config.toml (MCP guarded; native tools require rules/hooks)" : "Registered entrypoint is missing or stale; run `pi-charset doctor --fix`",
544
+ status: !runnable || missingApprovalMode ? "degraded" : "registered",
545
+ detail: !runnable
546
+ ? "Registered entrypoint is missing or stale; run `pi-charset doctor --fix`"
547
+ : missingApprovalMode
548
+ ? "Missing `default_tools_approval_mode` in ~/.codex/config.toml (Codex workspace-write/exec may block MCP calls); run `pi-charset doctor --fix`"
549
+ : `Registered in ~/.codex/config.toml (approval=${cur.entry.defaultToolsApprovalMode}; MCP guarded; native tools require rules/hooks)`,
462
550
  entrypoint: cur.entry.command && cur.entry.args ? { command: cur.entry.command, args: cur.entry.args } : undefined,
463
551
  });
464
552
  continue;
@@ -546,10 +634,10 @@ export function runSetup(opts: SetupOptions = {}): {
546
634
  results.push({ id, label: info.label, configPath: info.configPath, action: "skipped", status: "unregistered", detail: `No setup backup found for ${info.label}` });
547
635
  continue;
548
636
  }
549
- const prev = bak.previous as { command: string; args: string[] } | null;
637
+ const prev = bak.previous as { command: string; args: string[]; defaultToolsApprovalMode?: string } | null;
550
638
  const upd =
551
639
  id === "codex"
552
- ? updateCodexTomlConfig(info.configPath, backupRoot, prev, dryRun)
640
+ ? updateCodexTomlConfig(info.configPath, backupRoot, prev, dryRun, true)
553
641
  : updateJsonMcpConfig(id, info.configPath, backupRoot, prev, dryRun);
554
642
  results.push({
555
643
  id,
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Synchronized with package.json version.
2
- export const PLUGIN_VERSION = "0.1.8";
2
+ export const PLUGIN_VERSION = "0.1.9";
@@ -540,6 +540,7 @@ function runMcpConfig(args) {
540
540
  "[mcp_servers.pi-charset]",
541
541
  `command = ${JSON.stringify(command)}`,
542
542
  `args = ${JSON.stringify(fullArgs)}`,
543
+ 'default_tools_approval_mode = "approve"',
543
544
  ...(useLocal ? [] : [`# Direct local node alternative: command = "node", args = ${JSON.stringify([mcpBinPosix, ...serverFlags])}`]),
544
545
  ].join("\n"));
545
546
  }