@cydm/pie 4.10.1 → 4.10.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.
@@ -0,0 +1,251 @@
1
+ import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
2
+ import {
3
+ getConfigDir
4
+ } from "./chunk-CBDQJMUI.js";
5
+
6
+ // src/commands/manager-allow.ts
7
+ import * as fs from "node:fs";
8
+ import * as os from "node:os";
9
+ import * as path from "node:path";
10
+ import { randomBytes } from "node:crypto";
11
+ function expandTilde(p) {
12
+ return p === "~" ? os.homedir() : p.startsWith("~/") || p.startsWith("~\\") ? path.join(os.homedir(), p.slice(2)) : p;
13
+ }
14
+ function resolveProcConfigCandidates() {
15
+ const root = getConfigDir();
16
+ const candidates = [path.join(root, "pie-proc.json")];
17
+ const pinnedRoot = process.env.PIE_PROC_REGISTRY_ROOT;
18
+ if (pinnedRoot) {
19
+ const pinDerived = path.join(path.dirname(path.resolve(expandTilde(pinnedRoot))), "pie-proc.json");
20
+ if (!candidates.includes(pinDerived)) candidates.push(pinDerived);
21
+ } else {
22
+ const homeFile = path.join(os.homedir(), ".pie", "pie-proc.json");
23
+ if (!candidates.includes(homeFile)) candidates.push(homeFile);
24
+ }
25
+ return candidates;
26
+ }
27
+ function resolveEffectiveProcConfigFile() {
28
+ const candidates = resolveProcConfigCandidates();
29
+ for (const candidate of candidates) {
30
+ try {
31
+ if (fs.existsSync(candidate)) return { file: candidate, exists: true };
32
+ } catch {
33
+ return { file: candidate, exists: true };
34
+ }
35
+ }
36
+ return { file: candidates[0], exists: false };
37
+ }
38
+ function resolveWritableProcConfigFile() {
39
+ return path.join(getConfigDir(), "pie-proc.json");
40
+ }
41
+ function isTooBroadForWorkerSandbox(input) {
42
+ let resolved;
43
+ try {
44
+ resolved = fs.realpathSync(input);
45
+ } catch {
46
+ return true;
47
+ }
48
+ const root = path.parse(resolved).root;
49
+ if (resolved === root || resolved === os.homedir()) return true;
50
+ const forbidden = [os.homedir(), os.tmpdir(), "/tmp", "/var/tmp", "/private/tmp", "/private/var/tmp"];
51
+ for (const candidate of forbidden) {
52
+ try {
53
+ if (resolved === fs.realpathSync(candidate)) return true;
54
+ } catch {
55
+ }
56
+ }
57
+ try {
58
+ const stat = fs.statSync(resolved);
59
+ if ((stat.mode & 2) !== 0 && (stat.mode & 512) === 0) return true;
60
+ } catch {
61
+ return true;
62
+ }
63
+ return false;
64
+ }
65
+ function seedProcConfig(launchCwd) {
66
+ const root = getConfigDir();
67
+ const file = path.join(root, "pie-proc.json");
68
+ const resolved = path.resolve(launchCwd);
69
+ try {
70
+ if (fs.existsSync(file)) return { state: "exists", file, root };
71
+ } catch {
72
+ return { state: "exists", file, root };
73
+ }
74
+ const userLayer = path.join(os.homedir(), ".pie", "pie-proc.json");
75
+ try {
76
+ if (fs.existsSync(userLayer)) return { state: "user-layer", file: userLayer, root };
77
+ } catch {
78
+ }
79
+ if (isTooBroadForWorkerSandbox(resolved)) {
80
+ return { state: "too-broad", file, root };
81
+ }
82
+ fs.mkdirSync(root, { recursive: true });
83
+ fs.writeFileSync(file, `${JSON.stringify({ allowCwd: [resolved] }, null, " ")}
84
+ `);
85
+ return { state: "seeded", file, root };
86
+ }
87
+ function isTooBroadDir(canonical) {
88
+ return isTooBroadForWorkerSandbox(canonical);
89
+ }
90
+ function addAllowedWorkerCwd(rawDir, opts = {}) {
91
+ const resolved = path.resolve(expandTilde(rawDir));
92
+ let canonical;
93
+ try {
94
+ canonical = fs.realpathSync(resolved);
95
+ if (!fs.statSync(canonical).isDirectory()) {
96
+ throw new Error(`"${rawDir}" is not a directory.`);
97
+ }
98
+ } catch (error) {
99
+ throw new Error(
100
+ error instanceof Error && error.message.includes("not a directory") ? error.message : `Directory does not exist or is unreadable: "${rawDir}" (${resolved}).`
101
+ );
102
+ }
103
+ if (isTooBroadDir(canonical) && !opts.force) {
104
+ throw new Error(
105
+ `Refusing to authorize broad root "${canonical}" without --force. Pass a narrower project root (e.g. ~/Projects), or re-run with --force if you intentionally want this root.`
106
+ );
107
+ }
108
+ const target = opts.file ? path.resolve(expandTilde(opts.file)) : resolveWritableProcConfigFile();
109
+ const shadowedFallback = !opts.file && !fs.existsSync(target) ? resolveProcConfigCandidates().slice(1).find((c) => {
110
+ try {
111
+ return fs.existsSync(c);
112
+ } catch {
113
+ return false;
114
+ }
115
+ }) : void 0;
116
+ let doc = {};
117
+ let backedUpTo;
118
+ if (fs.existsSync(target)) {
119
+ const rawText = fs.readFileSync(target, "utf8");
120
+ try {
121
+ const parsed = JSON.parse(rawText);
122
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
123
+ throw new Error("top-level JSON value must be an object");
124
+ }
125
+ doc = { ...parsed };
126
+ } catch (error) {
127
+ const detail = error instanceof Error ? error.message : String(error);
128
+ if (!opts.backupCorrupt) {
129
+ throw new Error(
130
+ `Cannot update ${target}: invalid JSON (${detail}). Re-run with --backup-corrupt to back up the broken file and recreate it.`
131
+ );
132
+ }
133
+ backedUpTo = `${target}.corrupt-${Date.now()}-${randomBytes(3).toString("hex")}`;
134
+ fs.renameSync(target, backedUpTo);
135
+ doc = {};
136
+ }
137
+ }
138
+ const existing = Array.isArray(doc.allowCwd) ? doc.allowCwd.filter((v) => typeof v === "string" && v.trim().length > 0) : [];
139
+ const alreadyCovered = existing.some((entry) => {
140
+ try {
141
+ return fs.realpathSync(path.resolve(expandTilde(entry))) === canonical;
142
+ } catch {
143
+ return path.resolve(expandTilde(entry)) === canonical;
144
+ }
145
+ });
146
+ const nextAllowCwd = alreadyCovered ? existing : [...existing, canonical];
147
+ if (!alreadyCovered || backedUpTo) {
148
+ doc.allowCwd = nextAllowCwd;
149
+ fs.mkdirSync(path.dirname(target), { recursive: true });
150
+ const tmp = path.join(path.dirname(target), `.pie-proc-${process.pid}-${randomBytes(4).toString("hex")}.tmp`);
151
+ try {
152
+ fs.writeFileSync(tmp, `${JSON.stringify(doc, null, " ")}
153
+ `, { encoding: "utf8", mode: 384 });
154
+ fs.renameSync(tmp, target);
155
+ } finally {
156
+ try {
157
+ fs.rmSync(tmp, { force: true });
158
+ } catch {
159
+ }
160
+ }
161
+ }
162
+ return {
163
+ file: target,
164
+ canonicalDir: canonical,
165
+ added: !alreadyCovered,
166
+ allowCwd: nextAllowCwd,
167
+ ...backedUpTo ? { backedUpTo } : {},
168
+ ...shadowedFallback ? { shadowedFallback } : {}
169
+ };
170
+ }
171
+ function runManagerAllowCommand(args) {
172
+ const json = args.includes("--json");
173
+ const force = args.includes("--force");
174
+ const backupCorrupt = args.includes("--backup-corrupt");
175
+ const list = args.includes("--list");
176
+ const help = args.includes("--help") || args.includes("-h");
177
+ const fileIdx = args.indexOf("--file");
178
+ const explicitFile = fileIdx >= 0 ? args[fileIdx + 1] : void 0;
179
+ if (fileIdx >= 0 && (!explicitFile || explicitFile.startsWith("-"))) {
180
+ console.error("--file expects a pie-proc.json path.");
181
+ process.exitCode = 2;
182
+ return;
183
+ }
184
+ if (help) {
185
+ console.log(
186
+ "Usage:\n pie manager allow <dir> [--file <path>] [--force] [--backup-corrupt] [--json]\n pie manager allow --list [--json]\n\nAdds a canonical project root to the active pie-proc.json allowCwd list.\nRe-read on every worker spawn; no manager restart required."
187
+ );
188
+ return;
189
+ }
190
+ if (list) {
191
+ const { file, exists } = resolveEffectiveProcConfigFile();
192
+ if (!exists) {
193
+ if (json) console.log(JSON.stringify({ ok: true, file, exists: false, allowCwd: [] }));
194
+ else console.log(`No pie-proc.json found yet (next write target: ${file}).`);
195
+ return;
196
+ }
197
+ try {
198
+ const parsed = JSON.parse(fs.readFileSync(file, "utf8"));
199
+ const allowCwd = Array.isArray(parsed.allowCwd) ? parsed.allowCwd.filter((v) => typeof v === "string") : [];
200
+ if (json) console.log(JSON.stringify({ ok: true, file, exists: true, allowCwd, maxWorkers: parsed.maxWorkers ?? 50 }));
201
+ else {
202
+ console.log(`Policy file: ${file}`);
203
+ console.log(`maxWorkers: ${typeof parsed.maxWorkers === "number" ? parsed.maxWorkers : "50 (default)"}`);
204
+ console.log(allowCwd.length ? `allowCwd:
205
+ ${allowCwd.map((d) => ` - ${d}`).join("\n")}` : "allowCwd: (empty)");
206
+ }
207
+ } catch (error) {
208
+ const detail = error instanceof Error ? error.message : String(error);
209
+ if (json) console.log(JSON.stringify({ ok: false, file, error: detail }));
210
+ else console.error(`Invalid pie-proc.json at ${file}: ${detail}`);
211
+ process.exitCode = 1;
212
+ }
213
+ return;
214
+ }
215
+ const positional = [];
216
+ for (let i = 0; i < args.length; i += 1) {
217
+ if (args[i] === "--file") {
218
+ i += 1;
219
+ continue;
220
+ }
221
+ if (!args[i].startsWith("-")) positional.push(args[i]);
222
+ }
223
+ const targetDir = positional[0];
224
+ if (!targetDir) {
225
+ console.error("Usage: pie manager allow <dir> [--file <path>] [--force] [--backup-corrupt] [--json] | pie manager allow --list");
226
+ process.exitCode = 2;
227
+ return;
228
+ }
229
+ const result = addAllowedWorkerCwd(targetDir, { file: explicitFile, force, backupCorrupt });
230
+ if (json) {
231
+ console.log(JSON.stringify({ ok: true, ...result }));
232
+ return;
233
+ }
234
+ if (result.backedUpTo) console.log(`Backed up corrupt config to: ${result.backedUpTo}`);
235
+ if (result.shadowedFallback) {
236
+ console.log(`Note: created ${result.file}, which takes precedence over ${result.shadowedFallback}. Pass --file ${result.shadowedFallback} to update the shared policy instead.`);
237
+ }
238
+ console.log(
239
+ result.added ? `Authorized worker root "${result.canonicalDir}" in ${result.file} (effective immediately; no restart needed).` : `Worker root "${result.canonicalDir}" is already authorized in ${result.file}.`
240
+ );
241
+ }
242
+
243
+ export {
244
+ resolveProcConfigCandidates,
245
+ resolveEffectiveProcConfigFile,
246
+ resolveWritableProcConfigFile,
247
+ isTooBroadForWorkerSandbox,
248
+ seedProcConfig,
249
+ addAllowedWorkerCwd,
250
+ runManagerAllowCommand
251
+ };
@@ -0,0 +1,21 @@
1
+ import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
2
+ import {
3
+ addAllowedWorkerCwd,
4
+ isTooBroadForWorkerSandbox,
5
+ resolveEffectiveProcConfigFile,
6
+ resolveProcConfigCandidates,
7
+ resolveWritableProcConfigFile,
8
+ runManagerAllowCommand,
9
+ seedProcConfig
10
+ } from "./chunk-Z2RT3P4F.js";
11
+ import "./chunk-CBDQJMUI.js";
12
+ import "./chunk-LZQXQOPJ.js";
13
+ export {
14
+ addAllowedWorkerCwd,
15
+ isTooBroadForWorkerSandbox,
16
+ resolveEffectiveProcConfigFile,
17
+ resolveProcConfigCandidates,
18
+ resolveWritableProcConfigFile,
19
+ runManagerAllowCommand,
20
+ seedProcConfig
21
+ };
@@ -1,10 +1,12 @@
1
1
  import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
2
+ import {
3
+ isTooBroadForWorkerSandbox,
4
+ seedProcConfig
5
+ } from "./chunk-Z2RT3P4F.js";
2
6
  import {
3
7
  readProcessCommandLine
4
8
  } from "./chunk-7RJXKN7N.js";
5
- import {
6
- getConfigDir
7
- } from "./chunk-CBDQJMUI.js";
9
+ import "./chunk-CBDQJMUI.js";
8
10
  import "./chunk-LZQXQOPJ.js";
9
11
 
10
12
  // src/commands/manager-cli.ts
@@ -337,56 +339,6 @@ function seedWorkspace(dir) {
337
339
  fs2.appendFileSync(agents, "\n> Note: the resident role is now named manager. Update the role card manually if you want the latest template.\n");
338
340
  }
339
341
  }
340
- function isTooBroadForWorkerSandbox(input) {
341
- let resolved;
342
- try {
343
- resolved = fs2.realpathSync(input);
344
- } catch {
345
- return true;
346
- }
347
- const root = path2.parse(resolved).root;
348
- if (resolved === root || resolved === os.homedir()) return true;
349
- const forbidden = [os.homedir(), os.tmpdir(), "/tmp", "/var/tmp", "/private/tmp", "/private/var/tmp"];
350
- for (const candidate of forbidden) {
351
- try {
352
- const real = fs2.realpathSync(candidate);
353
- if (resolved === real) return true;
354
- } catch {
355
- }
356
- }
357
- try {
358
- const stat = fs2.statSync(resolved);
359
- if ((stat.mode & 2) !== 0 && (stat.mode & 512) === 0) return true;
360
- } catch {
361
- return true;
362
- }
363
- return false;
364
- }
365
- function seedProcConfig(launchCwd) {
366
- const root = getConfigDir();
367
- const file = path2.join(root, "pie-proc.json");
368
- const resolved = path2.resolve(launchCwd);
369
- try {
370
- if (fs2.existsSync(file)) return { state: "exists", file, root };
371
- } catch {
372
- return { state: "exists", file, root };
373
- }
374
- const userLayer = path2.join(os.homedir(), ".pie", "pie-proc.json");
375
- try {
376
- if (fs2.existsSync(userLayer)) return { state: "user-layer", file: userLayer, root };
377
- } catch {
378
- }
379
- if (isTooBroadForWorkerSandbox(resolved)) {
380
- return { state: "too-broad", file, root };
381
- }
382
- fs2.mkdirSync(root, { recursive: true });
383
- fs2.writeFileSync(
384
- file,
385
- `${JSON.stringify({ allowCwd: [resolved] }, null, " ")}
386
- `
387
- );
388
- return { state: "seeded", file, root };
389
- }
390
342
  function cliEntry() {
391
343
  return process.argv[1] ? path2.resolve(process.argv[1]) : "";
392
344
  }
@@ -1134,6 +1086,11 @@ async function runManagerCommand(args) {
1134
1086
  await runUpgradeCommand(args.slice(1));
1135
1087
  return;
1136
1088
  }
1089
+ if (sub === "allow") {
1090
+ const { runManagerAllowCommand } = await import("./manager-allow-SF5ROGDM.js");
1091
+ runManagerAllowCommand(args.slice(1));
1092
+ return;
1093
+ }
1137
1094
  if (sub === "tail") {
1138
1095
  const follow = args.includes("-f") || args.includes("--follow");
1139
1096
  const jsonOut = args.includes("--json");
@@ -1379,7 +1336,7 @@ async function runManagerCommand(args) {
1379
1336
  );
1380
1337
  } else if (procSeed.state === "too-broad") {
1381
1338
  console.log(
1382
- `Did not seed pie-proc config (the current directory ${path2.resolve(process.cwd())} is too broad). To dispatch work, write ${procSeed.file}: {"allowCwd": ["/your/project"]}`
1339
+ `Did not seed pie-proc config (the current directory ${path2.resolve(process.cwd())} is too broad). To dispatch work, run: pie manager allow /your/project (writes ${procSeed.file})`
1383
1340
  );
1384
1341
  }
1385
1342
  let readinessReport;