@cydm/pie 4.10.0 → 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.
- package/dist/builtin/extensions/im-forward/index.js +172 -53
- package/dist/builtin/extensions/mcp-adapter/index.js +216 -7
- package/dist/builtin/extensions/pie-proc/index.js +176 -146
- package/dist/chunks/chunk-BB7ETSJK.js +463 -0
- package/dist/chunks/chunk-Z2RT3P4F.js +251 -0
- package/dist/chunks/manager-allow-SF5ROGDM.js +21 -0
- package/dist/chunks/{manager-cli-Y6HM3MHA.js → manager-cli-BXI4UFTJ.js} +12 -55
- package/dist/chunks/{manager-upgrade-LSTOHBUV.js → manager-upgrade-XPFXKO7A.js} +64 -39
- package/dist/cli.js +295 -36
- package/package.json +1 -1
|
@@ -23,9 +23,9 @@ import "../../../chunks/chunk-LZQXQOPJ.js";
|
|
|
23
23
|
import { execFileSync, spawn } from "node:child_process";
|
|
24
24
|
import { createRequire } from "node:module";
|
|
25
25
|
import { fileURLToPath } from "node:url";
|
|
26
|
-
import * as
|
|
27
|
-
import * as
|
|
28
|
-
import * as
|
|
26
|
+
import * as fs4 from "node:fs";
|
|
27
|
+
import * as os2 from "node:os";
|
|
28
|
+
import * as path4 from "node:path";
|
|
29
29
|
|
|
30
30
|
// builtin/extensions/pie-proc/panel-bridge.ts
|
|
31
31
|
import * as fs from "node:fs";
|
|
@@ -148,9 +148,108 @@ function registerPieProcPanelBridge(deps) {
|
|
|
148
148
|
});
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
// builtin/extensions/pie-proc/
|
|
151
|
+
// builtin/extensions/pie-proc/proc-config.ts
|
|
152
152
|
import * as fs2 from "node:fs";
|
|
153
|
+
import * as os from "node:os";
|
|
153
154
|
import * as path2 from "node:path";
|
|
155
|
+
var DEFAULT_MAX_WORKERS = 50;
|
|
156
|
+
var DEFAULT_CONFIG = {
|
|
157
|
+
allowCwd: [],
|
|
158
|
+
maxWorkers: DEFAULT_MAX_WORKERS,
|
|
159
|
+
killGraceMs: 15e3,
|
|
160
|
+
defaultTtlMs: 0
|
|
161
|
+
};
|
|
162
|
+
function expandTilde(p) {
|
|
163
|
+
return p === "~" ? os.homedir() : p.startsWith("~/") ? path2.join(os.homedir(), p.slice(2)) : p;
|
|
164
|
+
}
|
|
165
|
+
function configDirRoot() {
|
|
166
|
+
const env = process.env.PIE_CODING_AGENT_DIR;
|
|
167
|
+
if (!env) return path2.join(os.homedir(), ".pie");
|
|
168
|
+
return path2.resolve(expandTilde(env));
|
|
169
|
+
}
|
|
170
|
+
function configCandidates() {
|
|
171
|
+
const candidates = [path2.join(configDirRoot(), "pie-proc.json")];
|
|
172
|
+
const pinnedRoot = process.env.PIE_PROC_REGISTRY_ROOT;
|
|
173
|
+
if (pinnedRoot) {
|
|
174
|
+
const pinDerived = path2.join(path2.dirname(path2.resolve(expandTilde(pinnedRoot))), "pie-proc.json");
|
|
175
|
+
if (!candidates.includes(pinDerived)) candidates.push(pinDerived);
|
|
176
|
+
} else if (configDirRoot() !== path2.join(os.homedir(), ".pie")) {
|
|
177
|
+
candidates.push(path2.join(os.homedir(), ".pie", "pie-proc.json"));
|
|
178
|
+
}
|
|
179
|
+
return candidates;
|
|
180
|
+
}
|
|
181
|
+
function readConfigDetailed() {
|
|
182
|
+
const candidates = configCandidates();
|
|
183
|
+
for (const file of candidates) {
|
|
184
|
+
try {
|
|
185
|
+
const parsed = JSON.parse(fs2.readFileSync(file, "utf8"));
|
|
186
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
187
|
+
throw new Error("top-level JSON value must be an object");
|
|
188
|
+
}
|
|
189
|
+
const raw = parsed;
|
|
190
|
+
return {
|
|
191
|
+
config: {
|
|
192
|
+
allowCwd: Array.isArray(raw.allowCwd) ? raw.allowCwd.map(expandTilde) : [],
|
|
193
|
+
maxWorkers: typeof raw.maxWorkers === "number" && raw.maxWorkers > 0 ? Math.floor(raw.maxWorkers) : DEFAULT_CONFIG.maxWorkers,
|
|
194
|
+
killGraceMs: typeof raw.killGraceMs === "number" && raw.killGraceMs >= 1e3 ? Math.floor(raw.killGraceMs) : DEFAULT_CONFIG.killGraceMs,
|
|
195
|
+
defaultTtlMs: typeof raw.defaultTtlMs === "number" && raw.defaultTtlMs >= 0 ? Math.floor(raw.defaultTtlMs) : DEFAULT_CONFIG.defaultTtlMs
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (fs2.existsSync(file)) {
|
|
200
|
+
return {
|
|
201
|
+
config: { ...DEFAULT_CONFIG },
|
|
202
|
+
loadError: { file, detail: error instanceof Error ? error.message : String(error) }
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return { config: { ...DEFAULT_CONFIG } };
|
|
208
|
+
}
|
|
209
|
+
function readConfig() {
|
|
210
|
+
return readConfigDetailed().config;
|
|
211
|
+
}
|
|
212
|
+
function canonicalDir(dir) {
|
|
213
|
+
const resolved = path2.resolve(expandTilde(dir));
|
|
214
|
+
const real = fs2.realpathSync(resolved);
|
|
215
|
+
const stat = fs2.statSync(real);
|
|
216
|
+
if (!stat.isDirectory()) throw new Error(`"${dir}" is not a directory`);
|
|
217
|
+
return real;
|
|
218
|
+
}
|
|
219
|
+
function cwdAllowedOrThrow(cwd, allowOrRead) {
|
|
220
|
+
const read = Array.isArray(allowOrRead) ? { config: { ...DEFAULT_CONFIG, allowCwd: allowOrRead } } : allowOrRead;
|
|
221
|
+
if (read.loadError) {
|
|
222
|
+
throw new Error(
|
|
223
|
+
`pie-proc policy at ${read.loadError.file} could not be loaded (${read.loadError.detail}); refusing to spawn. Run \`pie manager allow "${cwd}" --backup-corrupt\` to back up the broken file and authorize this root, or fix the JSON.`
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
const canonical = canonicalDir(cwd);
|
|
227
|
+
for (const root of read.config.allowCwd) {
|
|
228
|
+
try {
|
|
229
|
+
const canonicalRoot = canonicalDir(root);
|
|
230
|
+
if (canonical === canonicalRoot || canonical.startsWith(canonicalRoot + path2.sep)) return canonical;
|
|
231
|
+
} catch {
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const file = configCandidates()[0];
|
|
235
|
+
let exists = false;
|
|
236
|
+
try {
|
|
237
|
+
exists = fs2.existsSync(file);
|
|
238
|
+
} catch {
|
|
239
|
+
}
|
|
240
|
+
const effective = configCandidates().find((c) => {
|
|
241
|
+
try {
|
|
242
|
+
return fs2.existsSync(c);
|
|
243
|
+
} catch {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
}) ?? file;
|
|
247
|
+
throw new Error(exists || effective !== file ? `cwd "${cwd}" (canonical ${canonical}) is outside the pie-proc allowCwd roots in ${effective}. Run \`pie manager allow "${cwd}"\` (or edit that file) \u2014 it is re-read on every spawn, so no host restart is needed` : `cwd "${cwd}" is outside the pie-proc allowCwd roots: there is no config yet at ${effective}. Run \`pie manager allow "${cwd}"\` (or create ${effective} with {"allowCwd": ["${cwd}"]}) \u2014 it is read on every spawn, so no host restart is needed`);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// builtin/extensions/pie-proc/spawn-model.ts
|
|
251
|
+
import * as fs3 from "node:fs";
|
|
252
|
+
import * as path3 from "node:path";
|
|
154
253
|
var SPAWN_THINKING_LEVELS = /* @__PURE__ */ new Set(["off", "minimal", "low", "medium", "high", "xhigh", "max"]);
|
|
155
254
|
var MODEL_REF_RE = /^[^/\s]+\/\S+$/;
|
|
156
255
|
function collectModelRefsFromRaw(raw) {
|
|
@@ -182,7 +281,7 @@ function collectModelRefsFromRaw(raw) {
|
|
|
182
281
|
return refs;
|
|
183
282
|
}
|
|
184
283
|
function readKnownModelRefs(modelsPath) {
|
|
185
|
-
if (!
|
|
284
|
+
if (!fs3.existsSync(modelsPath)) return void 0;
|
|
186
285
|
try {
|
|
187
286
|
const resolved = loadResolvedModelsConfig(modelsPath);
|
|
188
287
|
const refs = collectModelRefsFromRaw(resolved.config);
|
|
@@ -190,7 +289,7 @@ function readKnownModelRefs(modelsPath) {
|
|
|
190
289
|
} catch {
|
|
191
290
|
}
|
|
192
291
|
try {
|
|
193
|
-
const raw = JSON.parse(
|
|
292
|
+
const raw = JSON.parse(fs3.readFileSync(modelsPath, "utf8"));
|
|
194
293
|
const refs = collectModelRefsFromRaw(raw);
|
|
195
294
|
return refs.size > 0 ? refs : void 0;
|
|
196
295
|
} catch {
|
|
@@ -212,8 +311,8 @@ function validateSpawnModelParams(opts) {
|
|
|
212
311
|
if (!MODEL_REF_RE.test(normalized)) {
|
|
213
312
|
throw new Error(`invalid model "${opts.model}" (expected "provider/modelId")`);
|
|
214
313
|
}
|
|
215
|
-
const workerModels =
|
|
216
|
-
const parentModels =
|
|
314
|
+
const workerModels = path3.join(opts.pieDir, "models.json");
|
|
315
|
+
const parentModels = path3.join(opts.parentConfigDir, "models.json");
|
|
217
316
|
const known = readKnownModelRefs(workerModels) ?? readKnownModelRefs(parentModels);
|
|
218
317
|
if (known && known.size > 0) {
|
|
219
318
|
if (known.has(normalized)) {
|
|
@@ -238,8 +337,8 @@ function validateSpawnModelParams(opts) {
|
|
|
238
337
|
}
|
|
239
338
|
function readSettingsDefaults(settingsPath) {
|
|
240
339
|
try {
|
|
241
|
-
if (!
|
|
242
|
-
const raw = JSON.parse(
|
|
340
|
+
if (!fs3.existsSync(settingsPath)) return {};
|
|
341
|
+
const raw = JSON.parse(fs3.readFileSync(settingsPath, "utf8"));
|
|
243
342
|
const provider = typeof raw.defaultProvider === "string" ? raw.defaultProvider.trim() : "";
|
|
244
343
|
const modelId = typeof raw.defaultModel === "string" ? raw.defaultModel.trim() : "";
|
|
245
344
|
const thinking = typeof raw.defaultThinkingLevel === "string" && SPAWN_THINKING_LEVELS.has(raw.defaultThinkingLevel.trim()) ? raw.defaultThinkingLevel.trim() : void 0;
|
|
@@ -255,7 +354,7 @@ function readModelsJsonDefault(modelsPath) {
|
|
|
255
354
|
const known = readKnownModelRefs(modelsPath);
|
|
256
355
|
if (!known || known.size === 0) return void 0;
|
|
257
356
|
try {
|
|
258
|
-
const raw = JSON.parse(
|
|
357
|
+
const raw = JSON.parse(fs3.readFileSync(modelsPath, "utf8"));
|
|
259
358
|
if (typeof raw.defaults?.provider === "string" && typeof raw.defaults?.modelId === "string") {
|
|
260
359
|
const ref = `${raw.defaults.provider.trim()}/${raw.defaults.modelId.trim()}`;
|
|
261
360
|
if (MODEL_REF_RE.test(ref)) return ref;
|
|
@@ -266,9 +365,9 @@ function readModelsJsonDefault(modelsPath) {
|
|
|
266
365
|
}
|
|
267
366
|
function readSessionHeadModel(sessionsDir, sessionId) {
|
|
268
367
|
try {
|
|
269
|
-
const headPath =
|
|
270
|
-
if (!
|
|
271
|
-
const parsed = JSON.parse(
|
|
368
|
+
const headPath = path3.join(sessionsDir, sessionId, "head.json");
|
|
369
|
+
if (!fs3.existsSync(headPath)) return {};
|
|
370
|
+
const parsed = JSON.parse(fs3.readFileSync(headPath, "utf8"));
|
|
272
371
|
const meta = parsed.metadata;
|
|
273
372
|
const provider = typeof meta?.provider === "string" ? meta.provider.trim() : "";
|
|
274
373
|
const modelId = typeof meta?.modelId === "string" ? meta.modelId.trim() : "";
|
|
@@ -282,9 +381,9 @@ function readSessionHeadModel(sessionsDir, sessionId) {
|
|
|
282
381
|
}
|
|
283
382
|
}
|
|
284
383
|
function predictWorkerModel(opts) {
|
|
285
|
-
const projectDefaults = readSettingsDefaults(
|
|
286
|
-
const workerDefaults = readSettingsDefaults(
|
|
287
|
-
const fallbackModel = readModelsJsonDefault(
|
|
384
|
+
const projectDefaults = readSettingsDefaults(path3.join(opts.cwd, ".pie", "settings.json"));
|
|
385
|
+
const workerDefaults = readSettingsDefaults(path3.join(opts.pieDir, "settings.json"));
|
|
386
|
+
const fallbackModel = readModelsJsonDefault(path3.join(opts.pieDir, "models.json"));
|
|
288
387
|
const model = opts.explicitModel ?? opts.resumed?.model ?? projectDefaults.model ?? workerDefaults.model ?? fallbackModel;
|
|
289
388
|
const thinkingLevel = opts.explicitThinking ?? opts.resumed?.thinkingLevel ?? projectDefaults.thinkingLevel ?? workerDefaults.thinkingLevel;
|
|
290
389
|
return { model, thinkingLevel };
|
|
@@ -300,12 +399,12 @@ function isPidAlive(pid) {
|
|
|
300
399
|
}
|
|
301
400
|
function readLiveWorkerPeer(ipcRoot, sessionId, pid) {
|
|
302
401
|
try {
|
|
303
|
-
const dir =
|
|
304
|
-
if (!
|
|
305
|
-
for (const file of
|
|
402
|
+
const dir = path3.join(ipcRoot, "run", "ipc");
|
|
403
|
+
if (!fs3.existsSync(dir)) return void 0;
|
|
404
|
+
for (const file of fs3.readdirSync(dir)) {
|
|
306
405
|
if (!file.endsWith(".json")) continue;
|
|
307
406
|
try {
|
|
308
|
-
const parsed = JSON.parse(
|
|
407
|
+
const parsed = JSON.parse(fs3.readFileSync(path3.join(dir, file), "utf8"));
|
|
309
408
|
if (parsed.pid !== pid || parsed.sessionId !== sessionId) continue;
|
|
310
409
|
if (!isPidAlive(pid)) return void 0;
|
|
311
410
|
return {
|
|
@@ -336,7 +435,7 @@ async function waitForWorkerIpcReady(opts) {
|
|
|
336
435
|
if (!isPidAlive(opts.pid)) return { exitedEarly: true };
|
|
337
436
|
const remaining = deadline - Date.now();
|
|
338
437
|
if (remaining <= 0) break;
|
|
339
|
-
await new Promise((
|
|
438
|
+
await new Promise((resolve3) => setTimeout(resolve3, Math.min(pollMs, remaining)));
|
|
340
439
|
}
|
|
341
440
|
return { peer: readLiveWorkerPeer(opts.ipcRoot, opts.sessionId, opts.pid), exitedEarly: !isPidAlive(opts.pid) };
|
|
342
441
|
}
|
|
@@ -365,11 +464,11 @@ async function buildSpawnReceipt(opts) {
|
|
|
365
464
|
}
|
|
366
465
|
function inheritWorkerConfigFiles(pieDir, parentConfigDir) {
|
|
367
466
|
for (const file of ["models.json", "config.json", "auth.json", "settings.json"]) {
|
|
368
|
-
const dst =
|
|
369
|
-
const src =
|
|
370
|
-
if (!
|
|
467
|
+
const dst = path3.join(pieDir, file);
|
|
468
|
+
const src = path3.join(parentConfigDir, file);
|
|
469
|
+
if (!fs3.existsSync(dst) && fs3.existsSync(src)) {
|
|
371
470
|
try {
|
|
372
|
-
|
|
471
|
+
fs3.copyFileSync(src, dst);
|
|
373
472
|
} catch {
|
|
374
473
|
}
|
|
375
474
|
}
|
|
@@ -392,64 +491,26 @@ function resolveOperatorSessionsDir() {
|
|
|
392
491
|
} catch {
|
|
393
492
|
const override = process.env.PIE_SESSIONS_DIR;
|
|
394
493
|
if (override !== void 0 && override !== "") {
|
|
395
|
-
if (override === "~") return
|
|
396
|
-
if (override.startsWith("~/")) return
|
|
397
|
-
if (
|
|
494
|
+
if (override === "~") return path4.join(os2.homedir(), ".pie", "sessions");
|
|
495
|
+
if (override.startsWith("~/")) return path4.join(os2.homedir(), override.slice(2));
|
|
496
|
+
if (path4.isAbsolute(override)) return override;
|
|
398
497
|
}
|
|
399
|
-
return
|
|
498
|
+
return path4.join(os2.homedir(), ".pie", "sessions");
|
|
400
499
|
}
|
|
401
500
|
}
|
|
402
|
-
var DEFAULT_MAX_WORKERS = 50;
|
|
403
|
-
var DEFAULT_CONFIG = { allowCwd: [], maxWorkers: DEFAULT_MAX_WORKERS, killGraceMs: 15e3, defaultTtlMs: 0 };
|
|
404
501
|
var IM_FORWARD_ALLOWED_KEYS = /* @__PURE__ */ new Set(["enabled", "provider", "chatId", "openId", "allowUsers", "retryBackoffMs", "larkCliProfile"]);
|
|
405
502
|
var ONESHOT_TASK_ENV = "PIE_PROC_ONESHOT_TASK";
|
|
406
503
|
var LAUNCH_TOKEN_ENV = "PIE_PROC_LAUNCH_TOKEN";
|
|
407
|
-
function configDirRoot() {
|
|
408
|
-
const env = process.env.PIE_CODING_AGENT_DIR;
|
|
409
|
-
if (!env) return path3.join(os.homedir(), ".pie");
|
|
410
|
-
return path3.resolve(expandTilde(env));
|
|
411
|
-
}
|
|
412
|
-
function expandTilde(p) {
|
|
413
|
-
return p === "~" ? os.homedir() : p.startsWith("~/") ? path3.join(os.homedir(), p.slice(2)) : p;
|
|
414
|
-
}
|
|
415
|
-
function configCandidates() {
|
|
416
|
-
const candidates = [path3.join(configDirRoot(), "pie-proc.json")];
|
|
417
|
-
const pinnedRoot = process.env.PIE_PROC_REGISTRY_ROOT;
|
|
418
|
-
if (pinnedRoot) {
|
|
419
|
-
const pinDerived = path3.join(path3.dirname(path3.resolve(expandTilde(pinnedRoot))), "pie-proc.json");
|
|
420
|
-
if (!candidates.includes(pinDerived)) candidates.push(pinDerived);
|
|
421
|
-
} else if (configDirRoot() !== path3.join(os.homedir(), ".pie")) {
|
|
422
|
-
candidates.push(path3.join(os.homedir(), ".pie", "pie-proc.json"));
|
|
423
|
-
}
|
|
424
|
-
return candidates;
|
|
425
|
-
}
|
|
426
|
-
function readConfig() {
|
|
427
|
-
const candidates = configCandidates();
|
|
428
|
-
for (const file of candidates) {
|
|
429
|
-
try {
|
|
430
|
-
const raw = JSON.parse(fs3.readFileSync(file, "utf8"));
|
|
431
|
-
return {
|
|
432
|
-
allowCwd: Array.isArray(raw.allowCwd) ? raw.allowCwd.map(expandTilde) : [],
|
|
433
|
-
maxWorkers: typeof raw.maxWorkers === "number" && raw.maxWorkers > 0 ? Math.floor(raw.maxWorkers) : DEFAULT_CONFIG.maxWorkers,
|
|
434
|
-
killGraceMs: typeof raw.killGraceMs === "number" && raw.killGraceMs >= 1e3 ? Math.floor(raw.killGraceMs) : DEFAULT_CONFIG.killGraceMs,
|
|
435
|
-
defaultTtlMs: typeof raw.defaultTtlMs === "number" && raw.defaultTtlMs >= 0 ? Math.floor(raw.defaultTtlMs) : DEFAULT_CONFIG.defaultTtlMs
|
|
436
|
-
};
|
|
437
|
-
} catch {
|
|
438
|
-
if (fs3.existsSync(file)) return { ...DEFAULT_CONFIG };
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return { ...DEFAULT_CONFIG };
|
|
442
|
-
}
|
|
443
504
|
function pieProcDir() {
|
|
444
505
|
const pinned = process.env.PIE_PROC_REGISTRY_ROOT;
|
|
445
|
-
if (pinned) return
|
|
446
|
-
return
|
|
506
|
+
if (pinned) return path4.resolve(expandTilde(pinned));
|
|
507
|
+
return path4.join(configDirRoot(), "pie-proc");
|
|
447
508
|
}
|
|
448
509
|
function registryFile() {
|
|
449
|
-
return
|
|
510
|
+
return path4.join(pieProcDir(), "workers.json");
|
|
450
511
|
}
|
|
451
512
|
function revisionFile(n) {
|
|
452
|
-
return
|
|
513
|
+
return path4.join(pieProcDir(), `workers.r${n}.json`);
|
|
453
514
|
}
|
|
454
515
|
function parseRegistry(rawText, file, revision) {
|
|
455
516
|
let raw;
|
|
@@ -481,7 +542,7 @@ function parseRegistry(rawText, file, revision) {
|
|
|
481
542
|
function latestRevisionOnDisk() {
|
|
482
543
|
let max = 0;
|
|
483
544
|
try {
|
|
484
|
-
for (const entry of
|
|
545
|
+
for (const entry of fs4.readdirSync(pieProcDir())) {
|
|
485
546
|
const match = /^workers\.r(\d+)\.json$/.exec(entry);
|
|
486
547
|
if (match) max = Math.max(max, Number(match[1]));
|
|
487
548
|
}
|
|
@@ -495,7 +556,7 @@ function readRegistryUncached() {
|
|
|
495
556
|
if (latest > 0) {
|
|
496
557
|
const file2 = revisionFile(latest);
|
|
497
558
|
try {
|
|
498
|
-
return parseRegistry(
|
|
559
|
+
return parseRegistry(fs4.readFileSync(file2, "utf8"), file2, latest);
|
|
499
560
|
} catch (error) {
|
|
500
561
|
if (error.code === "ENOENT") continue;
|
|
501
562
|
throw error;
|
|
@@ -503,7 +564,7 @@ function readRegistryUncached() {
|
|
|
503
564
|
}
|
|
504
565
|
const file = registryFile();
|
|
505
566
|
try {
|
|
506
|
-
return parseRegistry(
|
|
567
|
+
return parseRegistry(fs4.readFileSync(file, "utf8"), file, 0);
|
|
507
568
|
} catch (error) {
|
|
508
569
|
if (error.code === "ENOENT") return { revision: 0, workers: [] };
|
|
509
570
|
throw error;
|
|
@@ -514,7 +575,7 @@ function readRegistryUncached() {
|
|
|
514
575
|
function compactRevisions(_current) {
|
|
515
576
|
}
|
|
516
577
|
function casRegistry(mutate) {
|
|
517
|
-
|
|
578
|
+
fs4.mkdirSync(pieProcDir(), { recursive: true });
|
|
518
579
|
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
519
580
|
const snapshot = readRegistryUncached();
|
|
520
581
|
const outcome = mutate(snapshot);
|
|
@@ -525,26 +586,26 @@ function casRegistry(mutate) {
|
|
|
525
586
|
const nextRevision = snapshot.revision + 1;
|
|
526
587
|
const target = revisionFile(nextRevision);
|
|
527
588
|
const tmp = `${target}.${process.pid}.${Date.now().toString(36)}-${attempt}.tmp`;
|
|
528
|
-
|
|
589
|
+
fs4.writeFileSync(tmp, JSON.stringify({ version: 2, revision: nextRevision, workers: outcome.next }, null, " "));
|
|
529
590
|
if (latestRevisionOnDisk() > snapshot.revision) {
|
|
530
591
|
try {
|
|
531
|
-
|
|
592
|
+
fs4.rmSync(tmp, { force: true });
|
|
532
593
|
} catch {
|
|
533
594
|
}
|
|
534
595
|
continue;
|
|
535
596
|
}
|
|
536
597
|
try {
|
|
537
|
-
|
|
598
|
+
fs4.linkSync(tmp, target);
|
|
538
599
|
} catch (error) {
|
|
539
600
|
try {
|
|
540
|
-
|
|
601
|
+
fs4.rmSync(tmp, { force: true });
|
|
541
602
|
} catch {
|
|
542
603
|
}
|
|
543
604
|
if (error.code === "EEXIST") continue;
|
|
544
605
|
throw error;
|
|
545
606
|
}
|
|
546
607
|
try {
|
|
547
|
-
|
|
608
|
+
fs4.rmSync(tmp, { force: true });
|
|
548
609
|
} catch {
|
|
549
610
|
}
|
|
550
611
|
compactRevisions(nextRevision);
|
|
@@ -638,7 +699,7 @@ function verifyProcessEnvOwnership(pid, marker, pieDir) {
|
|
|
638
699
|
if (process.platform === "linux") {
|
|
639
700
|
let raw2;
|
|
640
701
|
try {
|
|
641
|
-
raw2 =
|
|
702
|
+
raw2 = fs4.readFileSync(`/proc/${pid}/environ`, "utf8");
|
|
642
703
|
} catch {
|
|
643
704
|
return "unverified";
|
|
644
705
|
}
|
|
@@ -684,7 +745,7 @@ function workerLiveness(record, opts = {}) {
|
|
|
684
745
|
if (groupProbe === "eperm") return "unknown";
|
|
685
746
|
const hasSpawnedAt = typeof record.spawnedAt === "number" && Number.isFinite(record.spawnedAt);
|
|
686
747
|
if (!hasSpawnedAt) return "unknown";
|
|
687
|
-
const bootAtMs = Date.now() -
|
|
748
|
+
const bootAtMs = Date.now() - os2.uptime() * 1e3;
|
|
688
749
|
if (record.spawnedAt < bootAtMs - 5e3) return "dead";
|
|
689
750
|
const members = readProcessGroupMembers(record.pid);
|
|
690
751
|
if (members === void 0) return "unknown";
|
|
@@ -707,37 +768,6 @@ function workerLiveness(record, opts = {}) {
|
|
|
707
768
|
}
|
|
708
769
|
return "dead";
|
|
709
770
|
}
|
|
710
|
-
function canonicalDir(dir) {
|
|
711
|
-
const resolved = path3.resolve(expandTilde(dir));
|
|
712
|
-
const real = fs3.realpathSync(resolved);
|
|
713
|
-
const stat = fs3.statSync(real);
|
|
714
|
-
if (!stat.isDirectory()) throw new Error(`"${dir}" is not a directory`);
|
|
715
|
-
return real;
|
|
716
|
-
}
|
|
717
|
-
function cwdAllowedOrThrow(cwd, allow) {
|
|
718
|
-
const canonical = canonicalDir(cwd);
|
|
719
|
-
for (const root of allow) {
|
|
720
|
-
try {
|
|
721
|
-
const canonicalRoot = canonicalDir(root);
|
|
722
|
-
if (canonical === canonicalRoot || canonical.startsWith(canonicalRoot + path3.sep)) return canonical;
|
|
723
|
-
} catch {
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
const file = configCandidates()[0];
|
|
727
|
-
let exists = false;
|
|
728
|
-
try {
|
|
729
|
-
exists = fs3.existsSync(file);
|
|
730
|
-
} catch {
|
|
731
|
-
}
|
|
732
|
-
const effective = configCandidates().find((c) => {
|
|
733
|
-
try {
|
|
734
|
-
return fs3.existsSync(c);
|
|
735
|
-
} catch {
|
|
736
|
-
return false;
|
|
737
|
-
}
|
|
738
|
-
}) ?? file;
|
|
739
|
-
throw new Error(exists || effective !== file ? `cwd "${cwd}" (canonical ${canonical}) is outside the pie-proc allowCwd roots in ${effective}; edit that file \u2014 it is re-read on every spawn, so no host restart is needed` : `cwd "${cwd}" is outside the pie-proc allowCwd roots: there is no config yet at ${effective}. Create it with {"allowCwd": ["${cwd}"]} \u2014 it is read on every spawn, so no host restart is needed`);
|
|
740
|
-
}
|
|
741
771
|
function safeName(name) {
|
|
742
772
|
return /^[a-z0-9][a-z0-9-_]{0,47}$/i.test(name);
|
|
743
773
|
}
|
|
@@ -762,10 +792,10 @@ function sanitizeImForward(raw) {
|
|
|
762
792
|
}
|
|
763
793
|
function readUserImForwardIdentity() {
|
|
764
794
|
try {
|
|
765
|
-
const candidates = [
|
|
795
|
+
const candidates = [path4.join(configDirRoot(), "im-forward.json")];
|
|
766
796
|
for (const file of candidates) {
|
|
767
797
|
try {
|
|
768
|
-
const parsed = JSON.parse(
|
|
798
|
+
const parsed = JSON.parse(fs4.readFileSync(file, "utf8"));
|
|
769
799
|
const provider = typeof parsed.provider === "string" ? parsed.provider : "feishu";
|
|
770
800
|
const settings = parsed.providers?.[provider];
|
|
771
801
|
if (!settings) continue;
|
|
@@ -782,12 +812,12 @@ function readUserImForwardIdentity() {
|
|
|
782
812
|
return void 0;
|
|
783
813
|
}
|
|
784
814
|
function cliEntry() {
|
|
785
|
-
const moduleDir =
|
|
815
|
+
const moduleDir = path4.dirname(fileURLToPath(import.meta.url));
|
|
786
816
|
for (const rel of ["../../../src/cli.ts", "../../../cli.js"]) {
|
|
787
|
-
const candidate =
|
|
817
|
+
const candidate = path4.resolve(moduleDir, rel);
|
|
788
818
|
try {
|
|
789
|
-
const real =
|
|
790
|
-
if (
|
|
819
|
+
const real = fs4.realpathSync(candidate);
|
|
820
|
+
if (fs4.statSync(real).isFile()) return real;
|
|
791
821
|
} catch {
|
|
792
822
|
}
|
|
793
823
|
}
|
|
@@ -797,10 +827,10 @@ function loaderArgs() {
|
|
|
797
827
|
for (let i = 0; i < process.execArgv.length - 1; i += 1) {
|
|
798
828
|
if (process.execArgv[i] === "--import") {
|
|
799
829
|
const target = process.execArgv[i + 1];
|
|
800
|
-
if (target && !
|
|
830
|
+
if (target && !path4.isAbsolute(target) && !target.startsWith("file:")) {
|
|
801
831
|
try {
|
|
802
|
-
const pkgRoot =
|
|
803
|
-
return ["--import",
|
|
832
|
+
const pkgRoot = path4.dirname(require_.resolve(`${target}/package.json`, { paths: [path4.dirname(cliEntry())] }));
|
|
833
|
+
return ["--import", path4.join(pkgRoot, "dist", "loader.mjs")];
|
|
804
834
|
} catch {
|
|
805
835
|
return [];
|
|
806
836
|
}
|
|
@@ -811,7 +841,7 @@ function loaderArgs() {
|
|
|
811
841
|
return [];
|
|
812
842
|
}
|
|
813
843
|
function logDir() {
|
|
814
|
-
return
|
|
844
|
+
return path4.join(pieProcDir(), "logs");
|
|
815
845
|
}
|
|
816
846
|
var toolText = (text, isError = false) => ({ content: [{ type: "text", text }], details: {}, ...isError ? { isError: true } : {} });
|
|
817
847
|
function pieProc(ctx) {
|
|
@@ -823,18 +853,18 @@ function pieProc(ctx) {
|
|
|
823
853
|
const observeActivity = (record) => {
|
|
824
854
|
const logTime = (() => {
|
|
825
855
|
try {
|
|
826
|
-
return
|
|
856
|
+
return fs4.statSync(path4.join(logDir(), `${record.name}.jsonl`)).mtimeMs;
|
|
827
857
|
} catch {
|
|
828
858
|
return record.lastActiveAt;
|
|
829
859
|
}
|
|
830
860
|
})();
|
|
831
861
|
if (record.pid === 0) return logTime;
|
|
832
|
-
const statusRoot =
|
|
862
|
+
const statusRoot = path4.join(path4.resolve(expandTilde(process.env.PIE_IPC_CONFIG_DIR ?? configDirRoot())), "run", "ipc");
|
|
833
863
|
try {
|
|
834
|
-
for (const file of
|
|
864
|
+
for (const file of fs4.readdirSync(statusRoot)) {
|
|
835
865
|
if (!file.endsWith(".json")) continue;
|
|
836
866
|
try {
|
|
837
|
-
const status = JSON.parse(
|
|
867
|
+
const status = JSON.parse(fs4.readFileSync(path4.join(statusRoot, file), "utf8"));
|
|
838
868
|
if (status.sessionId === record.sessionId && status.state === "working") return Date.now();
|
|
839
869
|
} catch {
|
|
840
870
|
}
|
|
@@ -997,20 +1027,20 @@ function pieProc(ctx) {
|
|
|
997
1027
|
if (params.kind === "oneshot" && params.imForward) throw new Error("imForward applies to resident workers only");
|
|
998
1028
|
if (params.kind !== "resident" && params.ttlMs !== void 0) throw new Error("ttlMs applies to resident workers only");
|
|
999
1029
|
if (params.kind !== "resident" && params.resumeSessionId) throw new Error("resumeSessionId applies to resident workers only");
|
|
1000
|
-
const canonicalCwd = cwdAllowedOrThrow(params.cwd,
|
|
1030
|
+
const canonicalCwd = cwdAllowedOrThrow(params.cwd, readConfigDetailed());
|
|
1001
1031
|
const entry = cliEntry();
|
|
1002
1032
|
if (!entry) throw new Error("cannot resolve a trusted pie entry point (refusing to spawn)");
|
|
1003
|
-
const validatedModel = validateSpawnModelParams({ model: params.model, thinkingLevel: params.thinkingLevel, pieDir:
|
|
1033
|
+
const validatedModel = validateSpawnModelParams({ model: params.model, thinkingLevel: params.thinkingLevel, pieDir: path4.join(pieProcDir(), "agents", params.name), parentConfigDir: configDirRoot() });
|
|
1004
1034
|
let resumeSessionId;
|
|
1005
1035
|
let resumeSessionsDir;
|
|
1006
1036
|
if (params.resumeSessionId) {
|
|
1007
1037
|
if (!/^[A-Za-z0-9][A-Za-z0-9-_]{5,63}$/.test(params.resumeSessionId)) throw new Error(`invalid resumeSessionId "${params.resumeSessionId}"`);
|
|
1008
1038
|
const operatorSessionsDir = resolveOperatorSessionsDir();
|
|
1009
|
-
const workerSessionsDir =
|
|
1039
|
+
const workerSessionsDir = path4.join(pieProcDir(), "agents", params.name, "sessions");
|
|
1010
1040
|
let recordedCwd;
|
|
1011
1041
|
for (const dir of [workerSessionsDir, operatorSessionsDir]) {
|
|
1012
1042
|
try {
|
|
1013
|
-
const head = JSON.parse(
|
|
1043
|
+
const head = JSON.parse(fs4.readFileSync(path4.join(dir, params.resumeSessionId, "head.json"), "utf8"));
|
|
1014
1044
|
recordedCwd = head.metadata?.cwd;
|
|
1015
1045
|
resumeSessionsDir = dir;
|
|
1016
1046
|
break;
|
|
@@ -1021,7 +1051,7 @@ function pieProc(ctx) {
|
|
|
1021
1051
|
if (typeof recordedCwd !== "string" || !recordedCwd) throw new Error(`session "${params.resumeSessionId}" has no recorded cwd; resume requires one`);
|
|
1022
1052
|
let canonicalRecorded = recordedCwd;
|
|
1023
1053
|
try {
|
|
1024
|
-
canonicalRecorded =
|
|
1054
|
+
canonicalRecorded = fs4.realpathSync(recordedCwd);
|
|
1025
1055
|
} catch {
|
|
1026
1056
|
}
|
|
1027
1057
|
if (canonicalRecorded !== canonicalCwd) throw new Error(`resume cwd mismatch: session recorded "${recordedCwd}" but spawn cwd is "${params.cwd}" \u2014 resume in the session's own directory`);
|
|
@@ -1098,12 +1128,12 @@ function pieProc(ctx) {
|
|
|
1098
1128
|
});
|
|
1099
1129
|
let child;
|
|
1100
1130
|
try {
|
|
1101
|
-
const pieDir =
|
|
1102
|
-
|
|
1131
|
+
const pieDir = path4.join(pieProcDir(), "agents", params.name);
|
|
1132
|
+
fs4.mkdirSync(pieDir, { recursive: true });
|
|
1103
1133
|
inheritWorkerConfigFiles(pieDir, configDirRoot());
|
|
1104
1134
|
if (imForward) {
|
|
1105
|
-
const imFile =
|
|
1106
|
-
if (!
|
|
1135
|
+
const imFile = path4.join(pieDir, "im-forward.json");
|
|
1136
|
+
if (!fs4.existsSync(imFile)) {
|
|
1107
1137
|
const provider = typeof imForward.provider === "string" && imForward.provider.trim() ? imForward.provider : "feishu";
|
|
1108
1138
|
const providerSettings = {};
|
|
1109
1139
|
for (const key of ["chatId", "openId", "allowUsers", "larkCliProfile"]) {
|
|
@@ -1115,15 +1145,15 @@ function pieProc(ctx) {
|
|
|
1115
1145
|
providers: { [provider]: providerSettings }
|
|
1116
1146
|
};
|
|
1117
1147
|
if (typeof imForward.retryBackoffMs === "number") doc.retryBackoffMs = imForward.retryBackoffMs;
|
|
1118
|
-
|
|
1148
|
+
fs4.writeFileSync(imFile, JSON.stringify(doc, null, " "));
|
|
1119
1149
|
}
|
|
1120
1150
|
}
|
|
1121
|
-
|
|
1122
|
-
const logFile =
|
|
1151
|
+
fs4.mkdirSync(logDir(), { recursive: true });
|
|
1152
|
+
const logFile = fs4.openSync(path4.join(logDir(), `${params.name}.jsonl`), "a");
|
|
1123
1153
|
const modelArgs = [...validatedModel.model ? ["--model", validatedModel.model] : [], ...validatedModel.thinkingLevel ? ["--thinking", validatedModel.thinkingLevel] : []];
|
|
1124
1154
|
const args = params.kind === "resident" ? [...loaderArgs(), entry, "--json-events", "--idle", "--session-id", sessionId, ...resumeSessionId ? ["--resume-required", "--worker-token", launchToken] : [], ...modelArgs] : [...loaderArgs(), entry, "--session-id", sessionId, ...modelArgs];
|
|
1125
|
-
const inheritedIpcRoot =
|
|
1126
|
-
const inheritedRegistryRoot =
|
|
1155
|
+
const inheritedIpcRoot = path4.resolve(expandTilde(process.env.PIE_IPC_CONFIG_DIR ?? configDirRoot()));
|
|
1156
|
+
const inheritedRegistryRoot = path4.resolve(expandTilde(process.env.PIE_PROC_REGISTRY_ROOT ?? pieProcDir()));
|
|
1127
1157
|
const { PIE_DEFAULT_PROVIDER: _p, PIE_DEFAULT_MODEL: _m, PIE_MANAGER_BOOTSTRAP: _b, PIE_PROC_CURATED_NAME: _c, ...inheritedEnv } = process.env;
|
|
1128
1158
|
const childEnv = {
|
|
1129
1159
|
...inheritedEnv,
|
|
@@ -1157,12 +1187,12 @@ function pieProc(ctx) {
|
|
|
1157
1187
|
});
|
|
1158
1188
|
} finally {
|
|
1159
1189
|
try {
|
|
1160
|
-
|
|
1190
|
+
fs4.closeSync(logFile);
|
|
1161
1191
|
} catch {
|
|
1162
1192
|
}
|
|
1163
1193
|
}
|
|
1164
1194
|
if (typeof child.pid !== "number") {
|
|
1165
|
-
throw new Error(`worker spawn failed (no pid) \u2014 see ${
|
|
1195
|
+
throw new Error(`worker spawn failed (no pid) \u2014 see ${path4.join(logDir(), `${params.name}.jsonl`)}`);
|
|
1166
1196
|
}
|
|
1167
1197
|
child.unref();
|
|
1168
1198
|
const record = {
|
|
@@ -1210,7 +1240,7 @@ function pieProc(ctx) {
|
|
|
1210
1240
|
cwd: record.cwd,
|
|
1211
1241
|
pieDir,
|
|
1212
1242
|
ipcRoot: inheritedIpcRoot,
|
|
1213
|
-
logPath:
|
|
1243
|
+
logPath: path4.join(logDir(), `${params.name}.jsonl`),
|
|
1214
1244
|
ttlMs: params.ttlMs,
|
|
1215
1245
|
explicitModel: validatedModel.model,
|
|
1216
1246
|
explicitThinking: validatedModel.thinkingLevel,
|
|
@@ -1338,7 +1368,7 @@ function pieProc(ctx) {
|
|
|
1338
1368
|
const action = args.action;
|
|
1339
1369
|
if (action === "list") {
|
|
1340
1370
|
const snap = readRegistryUncached();
|
|
1341
|
-
const ipcRoot =
|
|
1371
|
+
const ipcRoot = path4.resolve(expandTilde(process.env.PIE_IPC_CONFIG_DIR ?? configDirRoot()));
|
|
1342
1372
|
const rows = snap.workers.map((w) => {
|
|
1343
1373
|
const model = liveWorkerModelLabel(ipcRoot, w.sessionId, w.pid);
|
|
1344
1374
|
return { name: w.name, pid: w.pid, session: w.sessionId, cwd: w.cwd, kind: w.kind, ...model ? { model } : {}, status: w.pid === 0 ? `pending (reserver ${w.reserverPid ?? "?"})` : workerLiveness(w), idleSeconds: Math.round((Date.now() - Math.max(w.lastActiveAt, observeActivity(w))) / 1e3), ttlMs: w.kind === "resident" ? w.ttlMs ?? currentConfig().defaultTtlMs : 0, im: Boolean(w.imForward) };
|