@crewspaceai/adapter-utils 2026.412.0-canary.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.
@@ -0,0 +1,670 @@
1
+ import { spawn } from "node:child_process";
2
+ import { constants as fsConstants, promises as fs } from "node:fs";
3
+ import path from "node:path";
4
+ export const runningProcesses = new Map();
5
+ export const MAX_CAPTURE_BYTES = 4 * 1024 * 1024;
6
+ export const MAX_EXCERPT_BYTES = 32 * 1024;
7
+ const SENSITIVE_ENV_KEY = /(key|token|secret|password|passwd|authorization|cookie)/i;
8
+ const CREWSPACE_SKILL_ROOT_RELATIVE_CANDIDATES = [
9
+ "../../skills",
10
+ "../../../../../skills",
11
+ ];
12
+ function normalizePathSlashes(value) {
13
+ return value.replaceAll("\\", "/");
14
+ }
15
+ function isMaintainerOnlySkillTarget(candidate) {
16
+ return normalizePathSlashes(candidate).includes("/.agents/skills/");
17
+ }
18
+ function skillLocationLabel(value) {
19
+ if (typeof value !== "string")
20
+ return null;
21
+ const trimmed = value.trim();
22
+ return trimmed.length > 0 ? trimmed : null;
23
+ }
24
+ function buildManagedSkillOrigin(entry) {
25
+ if (entry.required) {
26
+ return {
27
+ origin: "crewspace_required",
28
+ originLabel: "Required by CrewSpace",
29
+ readOnly: false,
30
+ };
31
+ }
32
+ return {
33
+ origin: "company_managed",
34
+ originLabel: "Managed by CrewSpace",
35
+ readOnly: false,
36
+ };
37
+ }
38
+ function resolveInstalledEntryTarget(skillsHome, entryName, dirent, linkedPath) {
39
+ const fullPath = path.join(skillsHome, entryName);
40
+ if (dirent.isSymbolicLink()) {
41
+ return {
42
+ targetPath: linkedPath ? path.resolve(path.dirname(fullPath), linkedPath) : null,
43
+ kind: "symlink",
44
+ };
45
+ }
46
+ if (dirent.isDirectory()) {
47
+ return { targetPath: fullPath, kind: "directory" };
48
+ }
49
+ return { targetPath: fullPath, kind: "file" };
50
+ }
51
+ export function parseObject(value) {
52
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
53
+ return {};
54
+ }
55
+ return value;
56
+ }
57
+ export function asString(value, fallback) {
58
+ return typeof value === "string" && value.length > 0 ? value : fallback;
59
+ }
60
+ export function asNumber(value, fallback) {
61
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
62
+ }
63
+ export function asBoolean(value, fallback) {
64
+ return typeof value === "boolean" ? value : fallback;
65
+ }
66
+ export function asStringArray(value) {
67
+ return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
68
+ }
69
+ export function parseJson(value) {
70
+ try {
71
+ return JSON.parse(value);
72
+ }
73
+ catch {
74
+ return null;
75
+ }
76
+ }
77
+ export function appendWithCap(prev, chunk, cap = MAX_CAPTURE_BYTES) {
78
+ const combined = prev + chunk;
79
+ return combined.length > cap ? combined.slice(combined.length - cap) : combined;
80
+ }
81
+ export function resolvePathValue(obj, dottedPath) {
82
+ const parts = dottedPath.split(".");
83
+ let cursor = obj;
84
+ for (const part of parts) {
85
+ if (typeof cursor !== "object" || cursor === null || Array.isArray(cursor)) {
86
+ return "";
87
+ }
88
+ cursor = cursor[part];
89
+ }
90
+ if (cursor === null || cursor === undefined)
91
+ return "";
92
+ if (typeof cursor === "string")
93
+ return cursor;
94
+ if (typeof cursor === "number" || typeof cursor === "boolean")
95
+ return String(cursor);
96
+ try {
97
+ return JSON.stringify(cursor);
98
+ }
99
+ catch {
100
+ return "";
101
+ }
102
+ }
103
+ export function renderTemplate(template, data) {
104
+ return template.replace(/{{\s*([a-zA-Z0-9_.-]+)\s*}}/g, (_, path) => resolvePathValue(data, path));
105
+ }
106
+ export function joinPromptSections(sections, separator = "\n\n") {
107
+ return sections
108
+ .map((value) => (typeof value === "string" ? value.trim() : ""))
109
+ .filter(Boolean)
110
+ .join(separator);
111
+ }
112
+ export function redactEnvForLogs(env) {
113
+ const redacted = {};
114
+ for (const [key, value] of Object.entries(env)) {
115
+ redacted[key] = SENSITIVE_ENV_KEY.test(key) ? "***REDACTED***" : value;
116
+ }
117
+ return redacted;
118
+ }
119
+ export function buildInvocationEnvForLogs(env, options = {}) {
120
+ const merged = { ...env };
121
+ const runtimeEnv = options.runtimeEnv ?? {};
122
+ for (const key of options.includeRuntimeKeys ?? []) {
123
+ if (key in merged)
124
+ continue;
125
+ const value = runtimeEnv[key];
126
+ if (typeof value !== "string" || value.length === 0)
127
+ continue;
128
+ merged[key] = value;
129
+ }
130
+ const resolvedCommand = options.resolvedCommand?.trim();
131
+ if (resolvedCommand) {
132
+ merged[options.resolvedCommandEnvKey ?? "CREWSPACE_RESOLVED_COMMAND"] = resolvedCommand;
133
+ }
134
+ return redactEnvForLogs(merged);
135
+ }
136
+ export function buildCrewSpaceEnv(agent) {
137
+ const resolveHostForUrl = (rawHost) => {
138
+ const host = rawHost.trim();
139
+ if (!host || host === "0.0.0.0" || host === "::")
140
+ return "localhost";
141
+ if (host.includes(":") && !host.startsWith("[") && !host.endsWith("]"))
142
+ return `[${host}]`;
143
+ return host;
144
+ };
145
+ const vars = {
146
+ CREWSPACE_AGENT_ID: agent.id,
147
+ CREWSPACE_COMPANY_ID: agent.companyId,
148
+ };
149
+ const runtimeHost = resolveHostForUrl(process.env.CREWSPACE_LISTEN_HOST ?? process.env.HOST ?? "localhost");
150
+ const runtimePort = process.env.CREWSPACE_LISTEN_PORT ?? process.env.PORT ?? "3100";
151
+ const apiUrl = process.env.CREWSPACE_API_URL ?? `http://${runtimeHost}:${runtimePort}`;
152
+ vars.CREWSPACE_API_URL = apiUrl;
153
+ return vars;
154
+ }
155
+ export function defaultPathForPlatform() {
156
+ if (process.platform === "win32") {
157
+ return "C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\Wbem";
158
+ }
159
+ return "/usr/local/bin:/opt/homebrew/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin";
160
+ }
161
+ function windowsPathExts(env) {
162
+ return (env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean);
163
+ }
164
+ async function pathExists(candidate) {
165
+ try {
166
+ await fs.access(candidate, process.platform === "win32" ? fsConstants.F_OK : fsConstants.X_OK);
167
+ return true;
168
+ }
169
+ catch {
170
+ return false;
171
+ }
172
+ }
173
+ async function resolveCommandPath(command, cwd, env) {
174
+ const hasPathSeparator = command.includes("/") || command.includes("\\");
175
+ if (hasPathSeparator) {
176
+ const absolute = path.isAbsolute(command) ? command : path.resolve(cwd, command);
177
+ return (await pathExists(absolute)) ? absolute : null;
178
+ }
179
+ const pathValue = env.PATH ?? env.Path ?? "";
180
+ const delimiter = process.platform === "win32" ? ";" : ":";
181
+ const dirs = pathValue.split(delimiter).filter(Boolean);
182
+ const exts = process.platform === "win32" ? windowsPathExts(env) : [""];
183
+ const hasExtension = process.platform === "win32" && path.extname(command).length > 0;
184
+ for (const dir of dirs) {
185
+ const candidates = process.platform === "win32"
186
+ ? hasExtension
187
+ ? [path.join(dir, command)]
188
+ : exts.map((ext) => path.join(dir, `${command}${ext}`))
189
+ : [path.join(dir, command)];
190
+ for (const candidate of candidates) {
191
+ if (await pathExists(candidate))
192
+ return candidate;
193
+ }
194
+ }
195
+ return null;
196
+ }
197
+ export async function resolveCommandForLogs(command, cwd, env) {
198
+ return (await resolveCommandPath(command, cwd, env)) ?? command;
199
+ }
200
+ function quoteForCmd(arg) {
201
+ if (!arg.length)
202
+ return '""';
203
+ const escaped = arg.replace(/"/g, '""');
204
+ return /[\s"&<>|^()]/.test(escaped) ? `"${escaped}"` : escaped;
205
+ }
206
+ async function resolveSpawnTarget(command, args, cwd, env) {
207
+ const resolved = await resolveCommandPath(command, cwd, env);
208
+ const executable = resolved ?? command;
209
+ if (process.platform !== "win32") {
210
+ return { command: executable, args };
211
+ }
212
+ if (/\.(cmd|bat)$/i.test(executable)) {
213
+ const shell = env.ComSpec || process.env.ComSpec || "cmd.exe";
214
+ const commandLine = [quoteForCmd(executable), ...args.map(quoteForCmd)].join(" ");
215
+ return {
216
+ command: shell,
217
+ args: ["/d", "/s", "/c", commandLine],
218
+ };
219
+ }
220
+ return { command: executable, args };
221
+ }
222
+ export function ensurePathInEnv(env) {
223
+ if (typeof env.PATH === "string" && env.PATH.length > 0)
224
+ return env;
225
+ if (typeof env.Path === "string" && env.Path.length > 0)
226
+ return env;
227
+ return { ...env, PATH: defaultPathForPlatform() };
228
+ }
229
+ /**
230
+ * If `cwd` looks like a Windows absolute path (e.g. "C:\Users\...") and we are
231
+ * running on a non-Windows OS, it cannot be used — return `fallback` instead.
232
+ */
233
+ export function sanitizeCwd(cwd, fallback = process.cwd()) {
234
+ if (!cwd)
235
+ return fallback;
236
+ // Windows drive letter path: C:\ or C:/
237
+ if (process.platform !== "win32" && /^[A-Za-z]:[/\\]/.test(cwd)) {
238
+ return fallback;
239
+ }
240
+ return cwd;
241
+ }
242
+ export async function ensureAbsoluteDirectory(cwd, opts = {}) {
243
+ if (!path.isAbsolute(cwd)) {
244
+ throw new Error(`Working directory must be an absolute path: "${cwd}"`);
245
+ }
246
+ const assertDirectory = async () => {
247
+ const stats = await fs.stat(cwd);
248
+ if (!stats.isDirectory()) {
249
+ throw new Error(`Working directory is not a directory: "${cwd}"`);
250
+ }
251
+ };
252
+ try {
253
+ await assertDirectory();
254
+ return;
255
+ }
256
+ catch (err) {
257
+ const code = err.code;
258
+ if (!opts.createIfMissing || code !== "ENOENT") {
259
+ if (code === "ENOENT") {
260
+ throw new Error(`Working directory does not exist: "${cwd}"`);
261
+ }
262
+ throw err instanceof Error ? err : new Error(String(err));
263
+ }
264
+ }
265
+ try {
266
+ await fs.mkdir(cwd, { recursive: true });
267
+ await assertDirectory();
268
+ }
269
+ catch (err) {
270
+ const reason = err instanceof Error ? err.message : String(err);
271
+ throw new Error(`Could not create working directory "${cwd}": ${reason}`);
272
+ }
273
+ }
274
+ export async function resolveCrewSpaceSkillsDir(moduleDir, additionalCandidates = []) {
275
+ const candidates = [
276
+ ...CREWSPACE_SKILL_ROOT_RELATIVE_CANDIDATES.map((relativePath) => path.resolve(moduleDir, relativePath)),
277
+ ...additionalCandidates.map((candidate) => path.resolve(candidate)),
278
+ ];
279
+ const seenRoots = new Set();
280
+ for (const root of candidates) {
281
+ if (seenRoots.has(root))
282
+ continue;
283
+ seenRoots.add(root);
284
+ const isDirectory = await fs.stat(root).then((stats) => stats.isDirectory()).catch(() => false);
285
+ if (isDirectory)
286
+ return root;
287
+ }
288
+ return null;
289
+ }
290
+ export async function listCrewSpaceSkillEntries(moduleDir, additionalCandidates = []) {
291
+ const root = await resolveCrewSpaceSkillsDir(moduleDir, additionalCandidates);
292
+ if (!root)
293
+ return [];
294
+ try {
295
+ const entries = await fs.readdir(root, { withFileTypes: true });
296
+ return entries
297
+ .filter((entry) => entry.isDirectory())
298
+ .map((entry) => ({
299
+ key: `crewspaceai/crewspace/${entry.name}`,
300
+ runtimeName: entry.name,
301
+ source: path.join(root, entry.name),
302
+ required: true,
303
+ requiredReason: "Bundled CrewSpace skills are always available for local adapters.",
304
+ }));
305
+ }
306
+ catch {
307
+ return [];
308
+ }
309
+ }
310
+ export async function readInstalledSkillTargets(skillsHome) {
311
+ const entries = await fs.readdir(skillsHome, { withFileTypes: true }).catch(() => []);
312
+ const out = new Map();
313
+ for (const entry of entries) {
314
+ const fullPath = path.join(skillsHome, entry.name);
315
+ const linkedPath = entry.isSymbolicLink() ? await fs.readlink(fullPath).catch(() => null) : null;
316
+ out.set(entry.name, resolveInstalledEntryTarget(skillsHome, entry.name, entry, linkedPath));
317
+ }
318
+ return out;
319
+ }
320
+ export function buildPersistentSkillSnapshot(options) {
321
+ const { adapterType, availableEntries, desiredSkills, installed, skillsHome, locationLabel, installedDetail, missingDetail, externalConflictDetail, externalDetail, } = options;
322
+ const availableByKey = new Map(availableEntries.map((entry) => [entry.key, entry]));
323
+ const desiredSet = new Set(desiredSkills);
324
+ const entries = [];
325
+ const warnings = [...(options.warnings ?? [])];
326
+ for (const available of availableEntries) {
327
+ const installedEntry = installed.get(available.runtimeName) ?? null;
328
+ const desired = desiredSet.has(available.key);
329
+ let state = "available";
330
+ let managed = false;
331
+ let detail = null;
332
+ if (installedEntry?.targetPath === available.source) {
333
+ managed = true;
334
+ state = desired ? "installed" : "stale";
335
+ detail = installedDetail ?? null;
336
+ }
337
+ else if (installedEntry) {
338
+ state = "external";
339
+ detail = desired ? externalConflictDetail : externalDetail;
340
+ }
341
+ else if (desired) {
342
+ state = "missing";
343
+ detail = missingDetail;
344
+ }
345
+ entries.push({
346
+ key: available.key,
347
+ runtimeName: available.runtimeName,
348
+ desired,
349
+ managed,
350
+ state,
351
+ sourcePath: available.source,
352
+ targetPath: path.join(skillsHome, available.runtimeName),
353
+ detail,
354
+ required: Boolean(available.required),
355
+ requiredReason: available.requiredReason ?? null,
356
+ ...buildManagedSkillOrigin(available),
357
+ });
358
+ }
359
+ for (const desiredSkill of desiredSkills) {
360
+ if (availableByKey.has(desiredSkill))
361
+ continue;
362
+ warnings.push(`Desired skill "${desiredSkill}" is not available from the CrewSpace skills directory.`);
363
+ entries.push({
364
+ key: desiredSkill,
365
+ runtimeName: null,
366
+ desired: true,
367
+ managed: true,
368
+ state: "missing",
369
+ sourcePath: null,
370
+ targetPath: null,
371
+ detail: "CrewSpace cannot find this skill in the local runtime skills directory.",
372
+ origin: "external_unknown",
373
+ originLabel: "External or unavailable",
374
+ readOnly: false,
375
+ });
376
+ }
377
+ for (const [name, installedEntry] of installed.entries()) {
378
+ if (availableEntries.some((entry) => entry.runtimeName === name))
379
+ continue;
380
+ entries.push({
381
+ key: name,
382
+ runtimeName: name,
383
+ desired: false,
384
+ managed: false,
385
+ state: "external",
386
+ origin: "user_installed",
387
+ originLabel: "User-installed",
388
+ locationLabel: skillLocationLabel(locationLabel),
389
+ readOnly: true,
390
+ sourcePath: null,
391
+ targetPath: installedEntry.targetPath ?? path.join(skillsHome, name),
392
+ detail: externalDetail,
393
+ });
394
+ }
395
+ entries.sort((left, right) => left.key.localeCompare(right.key));
396
+ return {
397
+ adapterType,
398
+ supported: true,
399
+ mode: "persistent",
400
+ desiredSkills,
401
+ entries,
402
+ warnings,
403
+ };
404
+ }
405
+ function normalizeConfiguredCrewSpaceRuntimeSkills(value) {
406
+ if (!Array.isArray(value))
407
+ return [];
408
+ const out = [];
409
+ for (const rawEntry of value) {
410
+ const entry = parseObject(rawEntry);
411
+ const key = asString(entry.key, asString(entry.name, "")).trim();
412
+ const runtimeName = asString(entry.runtimeName, asString(entry.name, "")).trim();
413
+ const source = asString(entry.source, "").trim();
414
+ if (!key || !runtimeName || !source)
415
+ continue;
416
+ out.push({
417
+ key,
418
+ runtimeName,
419
+ source,
420
+ required: asBoolean(entry.required, false),
421
+ requiredReason: typeof entry.requiredReason === "string" && entry.requiredReason.trim().length > 0
422
+ ? entry.requiredReason.trim()
423
+ : null,
424
+ });
425
+ }
426
+ return out;
427
+ }
428
+ export async function readCrewSpaceRuntimeSkillEntries(config, moduleDir, additionalCandidates = []) {
429
+ const configuredEntries = normalizeConfiguredCrewSpaceRuntimeSkills(config.crewspaceRuntimeSkills);
430
+ if (configuredEntries.length > 0)
431
+ return configuredEntries;
432
+ return listCrewSpaceSkillEntries(moduleDir, additionalCandidates);
433
+ }
434
+ export async function readCrewSpaceSkillMarkdown(moduleDir, skillKey) {
435
+ const normalized = skillKey.trim().toLowerCase();
436
+ if (!normalized)
437
+ return null;
438
+ const entries = await listCrewSpaceSkillEntries(moduleDir);
439
+ const match = entries.find((entry) => entry.key === normalized);
440
+ if (!match)
441
+ return null;
442
+ try {
443
+ return await fs.readFile(path.join(match.source, "SKILL.md"), "utf8");
444
+ }
445
+ catch {
446
+ return null;
447
+ }
448
+ }
449
+ export function readCrewSpaceSkillSyncPreference(config) {
450
+ const raw = config.crewspaceSkillSync;
451
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
452
+ return { explicit: false, desiredSkills: [] };
453
+ }
454
+ const syncConfig = raw;
455
+ const desiredValues = syncConfig.desiredSkills;
456
+ const desired = Array.isArray(desiredValues)
457
+ ? desiredValues
458
+ .filter((value) => typeof value === "string")
459
+ .map((value) => value.trim())
460
+ .filter(Boolean)
461
+ : [];
462
+ return {
463
+ explicit: Object.prototype.hasOwnProperty.call(raw, "desiredSkills"),
464
+ desiredSkills: Array.from(new Set(desired)),
465
+ };
466
+ }
467
+ function canonicalizeDesiredCrewSpaceSkillReference(reference, availableEntries) {
468
+ const normalizedReference = reference.trim().toLowerCase();
469
+ if (!normalizedReference)
470
+ return "";
471
+ const exactKey = availableEntries.find((entry) => entry.key.trim().toLowerCase() === normalizedReference);
472
+ if (exactKey)
473
+ return exactKey.key;
474
+ const byRuntimeName = availableEntries.filter((entry) => typeof entry.runtimeName === "string" && entry.runtimeName.trim().toLowerCase() === normalizedReference);
475
+ if (byRuntimeName.length === 1)
476
+ return byRuntimeName[0].key;
477
+ const slugMatches = availableEntries.filter((entry) => entry.key.trim().toLowerCase().split("/").pop() === normalizedReference);
478
+ if (slugMatches.length === 1)
479
+ return slugMatches[0].key;
480
+ return normalizedReference;
481
+ }
482
+ export function resolveCrewSpaceDesiredSkillNames(config, availableEntries) {
483
+ const preference = readCrewSpaceSkillSyncPreference(config);
484
+ const requiredSkills = availableEntries
485
+ .filter((entry) => entry.required)
486
+ .map((entry) => entry.key);
487
+ if (!preference.explicit) {
488
+ return Array.from(new Set(requiredSkills));
489
+ }
490
+ const desiredSkills = preference.desiredSkills
491
+ .map((reference) => canonicalizeDesiredCrewSpaceSkillReference(reference, availableEntries))
492
+ .filter(Boolean);
493
+ return Array.from(new Set([...requiredSkills, ...desiredSkills]));
494
+ }
495
+ export function writeCrewSpaceSkillSyncPreference(config, desiredSkills) {
496
+ const next = { ...config };
497
+ const raw = next.crewspaceSkillSync;
498
+ const current = typeof raw === "object" && raw !== null && !Array.isArray(raw)
499
+ ? { ...raw }
500
+ : {};
501
+ current.desiredSkills = Array.from(new Set(desiredSkills
502
+ .map((value) => value.trim())
503
+ .filter(Boolean)));
504
+ next.crewspaceSkillSync = current;
505
+ return next;
506
+ }
507
+ export async function ensureCrewSpaceSkillSymlink(source, target, linkSkill = (linkSource, linkTarget) => fs.symlink(linkSource, linkTarget)) {
508
+ const existing = await fs.lstat(target).catch(() => null);
509
+ if (!existing) {
510
+ await linkSkill(source, target);
511
+ return "created";
512
+ }
513
+ if (!existing.isSymbolicLink()) {
514
+ return "skipped";
515
+ }
516
+ const linkedPath = await fs.readlink(target).catch(() => null);
517
+ if (!linkedPath)
518
+ return "skipped";
519
+ const resolvedLinkedPath = path.resolve(path.dirname(target), linkedPath);
520
+ if (resolvedLinkedPath === source) {
521
+ return "skipped";
522
+ }
523
+ const linkedPathExists = await fs.stat(resolvedLinkedPath).then(() => true).catch(() => false);
524
+ if (linkedPathExists) {
525
+ return "skipped";
526
+ }
527
+ await fs.unlink(target);
528
+ await linkSkill(source, target);
529
+ return "repaired";
530
+ }
531
+ export async function removeMaintainerOnlySkillSymlinks(skillsHome, allowedSkillNames) {
532
+ const allowed = new Set(Array.from(allowedSkillNames));
533
+ try {
534
+ const entries = await fs.readdir(skillsHome, { withFileTypes: true });
535
+ const removed = [];
536
+ for (const entry of entries) {
537
+ if (allowed.has(entry.name))
538
+ continue;
539
+ const target = path.join(skillsHome, entry.name);
540
+ const existing = await fs.lstat(target).catch(() => null);
541
+ if (!existing?.isSymbolicLink())
542
+ continue;
543
+ const linkedPath = await fs.readlink(target).catch(() => null);
544
+ if (!linkedPath)
545
+ continue;
546
+ const resolvedLinkedPath = path.isAbsolute(linkedPath)
547
+ ? linkedPath
548
+ : path.resolve(path.dirname(target), linkedPath);
549
+ if (!isMaintainerOnlySkillTarget(linkedPath) &&
550
+ !isMaintainerOnlySkillTarget(resolvedLinkedPath)) {
551
+ continue;
552
+ }
553
+ await fs.unlink(target);
554
+ removed.push(entry.name);
555
+ }
556
+ return removed;
557
+ }
558
+ catch {
559
+ return [];
560
+ }
561
+ }
562
+ export async function ensureCommandResolvable(command, cwd, env) {
563
+ const resolved = await resolveCommandPath(command, cwd, env);
564
+ if (resolved)
565
+ return;
566
+ if (command.includes("/") || command.includes("\\")) {
567
+ const absolute = path.isAbsolute(command) ? command : path.resolve(cwd, command);
568
+ throw new Error(`Command is not executable: "${command}" (resolved: "${absolute}")`);
569
+ }
570
+ throw new Error(`Command not found in PATH: "${command}"`);
571
+ }
572
+ export async function runChildProcess(runId, command, args, opts) {
573
+ const onLogError = opts.onLogError ?? ((err, id, msg) => console.warn({ err, runId: id }, msg));
574
+ return new Promise((resolve, reject) => {
575
+ const rawMerged = { ...process.env, ...opts.env };
576
+ // Strip Claude Code nesting-guard env vars so spawned `claude` processes
577
+ // don't refuse to start with "cannot be launched inside another session".
578
+ // These vars leak in when the CrewSpace server itself is started from
579
+ // within a Claude Code session (e.g. `npx crewspaceai run` in a terminal
580
+ // owned by Claude Code) or when cron inherits a contaminated shell env.
581
+ const CLAUDE_CODE_NESTING_VARS = [
582
+ "CLAUDECODE",
583
+ "CLAUDE_CODE_ENTRYPOINT",
584
+ "CLAUDE_CODE_SESSION",
585
+ "CLAUDE_CODE_PARENT_SESSION",
586
+ ];
587
+ for (const key of CLAUDE_CODE_NESTING_VARS) {
588
+ delete rawMerged[key];
589
+ }
590
+ const mergedEnv = ensurePathInEnv(rawMerged);
591
+ void resolveSpawnTarget(command, args, opts.cwd, mergedEnv)
592
+ .then((target) => {
593
+ const child = spawn(target.command, target.args, {
594
+ cwd: opts.cwd,
595
+ env: mergedEnv,
596
+ shell: false,
597
+ stdio: [opts.stdin != null ? "pipe" : "ignore", "pipe", "pipe"],
598
+ });
599
+ const startedAt = new Date().toISOString();
600
+ if (opts.stdin != null && child.stdin) {
601
+ child.stdin.write(opts.stdin);
602
+ child.stdin.end();
603
+ }
604
+ if (typeof child.pid === "number" && child.pid > 0 && opts.onSpawn) {
605
+ void opts.onSpawn({ pid: child.pid, startedAt }).catch((err) => {
606
+ onLogError(err, runId, "failed to record child process metadata");
607
+ });
608
+ }
609
+ runningProcesses.set(runId, { child, graceSec: opts.graceSec });
610
+ let timedOut = false;
611
+ let stdout = "";
612
+ let stderr = "";
613
+ let logChain = Promise.resolve();
614
+ const timeout = opts.timeoutSec > 0
615
+ ? setTimeout(() => {
616
+ timedOut = true;
617
+ child.kill("SIGTERM");
618
+ setTimeout(() => {
619
+ if (!child.killed) {
620
+ child.kill("SIGKILL");
621
+ }
622
+ }, Math.max(1, opts.graceSec) * 1000);
623
+ }, opts.timeoutSec * 1000)
624
+ : null;
625
+ child.stdout?.on("data", (chunk) => {
626
+ const text = String(chunk);
627
+ stdout = appendWithCap(stdout, text);
628
+ logChain = logChain
629
+ .then(() => opts.onLog("stdout", text))
630
+ .catch((err) => onLogError(err, runId, "failed to append stdout log chunk"));
631
+ });
632
+ child.stderr?.on("data", (chunk) => {
633
+ const text = String(chunk);
634
+ stderr = appendWithCap(stderr, text);
635
+ logChain = logChain
636
+ .then(() => opts.onLog("stderr", text))
637
+ .catch((err) => onLogError(err, runId, "failed to append stderr log chunk"));
638
+ });
639
+ child.on("error", (err) => {
640
+ if (timeout)
641
+ clearTimeout(timeout);
642
+ runningProcesses.delete(runId);
643
+ const errno = err.code;
644
+ const pathValue = mergedEnv.PATH ?? mergedEnv.Path ?? "";
645
+ const msg = errno === "ENOENT"
646
+ ? `Failed to start command "${command}" in "${opts.cwd}". Verify adapter command, working directory, and PATH (${pathValue}).`
647
+ : `Failed to start command "${command}" in "${opts.cwd}": ${err.message}`;
648
+ reject(new Error(msg));
649
+ });
650
+ child.on("close", (code, signal) => {
651
+ if (timeout)
652
+ clearTimeout(timeout);
653
+ runningProcesses.delete(runId);
654
+ void logChain.finally(() => {
655
+ resolve({
656
+ exitCode: code,
657
+ signal,
658
+ timedOut,
659
+ stdout,
660
+ stderr,
661
+ pid: child.pid ?? null,
662
+ startedAt,
663
+ });
664
+ });
665
+ });
666
+ })
667
+ .catch(reject);
668
+ });
669
+ }
670
+ //# sourceMappingURL=server-utils.js.map