@causyne/agent 1.20.22 → 1.20.25

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.
@@ -10,7 +10,9 @@
10
10
  * 4. Ring-buffered output truncation (50KB cap) to protect LLM context windows.
11
11
  */
12
12
  import { spawn } from 'node:child_process';
13
+ import fs from 'node:fs';
13
14
  import os from 'node:os';
15
+ import path from 'node:path';
14
16
  import { isLaunchOnlyPrompt, isServerLaunchCommand } from '../launchIntent.js';
15
17
  /**
16
18
  * Strips ANSI escape sequences and terminal control codes from string output.
@@ -39,7 +41,78 @@ export function cleanSubprocessOutput(str) {
39
41
  * Commands whose exit code 1 indicates a normal query result (e.g. no matches found,
40
42
  * differences found, or condition evaluated to false) rather than a system error.
41
43
  */
42
- export const SEARCH_COMMAND_PATTERN = /(^|\s|\||&&|;)(grep|egrep|fgrep|diff|test|which|cmp|find)(\s|$)/;
44
+ export const SEARCH_COMMAND_PATTERN = /(^|\s|\||&&|;)(grep|egrep|fgrep|diff|test|which|cmp|find|pidof|pgrep)(\s|$)/;
45
+ /**
46
+ * Resolves an enriched environment for subprocess execution with full toolchain
47
+ * awareness. Automatically discovers Android SDK, Homebrew, local user binaries,
48
+ * and language runtimes so ADB, emulator, and build tools function out-of-the-box.
49
+ */
50
+ export function getEnhancedEnv(baseEnv) {
51
+ const env = { ...(baseEnv || process.env) };
52
+ // 1. Discover ANDROID_HOME / ANDROID_SDK_ROOT across standard platform locations
53
+ if (!env.ANDROID_HOME && !env.ANDROID_SDK_ROOT) {
54
+ const candidateDirs = [
55
+ path.join(os.homedir(), 'Library', 'Android', 'sdk'), // macOS
56
+ path.join(os.homedir(), 'Android', 'Sdk'), // Linux
57
+ '/usr/lib/android-sdk', // Linux package
58
+ path.join(process.env.LOCALAPPDATA || '', 'Android', 'Sdk') // Windows
59
+ ];
60
+ for (const dir of candidateDirs) {
61
+ if (dir && fs.existsSync(dir)) {
62
+ env.ANDROID_HOME = dir;
63
+ env.ANDROID_SDK_ROOT = dir;
64
+ break;
65
+ }
66
+ }
67
+ }
68
+ else if (!env.ANDROID_HOME && env.ANDROID_SDK_ROOT) {
69
+ env.ANDROID_HOME = env.ANDROID_SDK_ROOT;
70
+ }
71
+ else if (env.ANDROID_HOME && !env.ANDROID_SDK_ROOT) {
72
+ env.ANDROID_SDK_ROOT = env.ANDROID_HOME;
73
+ }
74
+ // 2. Build enriched PATH including dev tools, package managers, and Android toolchains
75
+ const currentPath = env.PATH || '';
76
+ const pathParts = currentPath.split(path.delimiter).filter(Boolean);
77
+ const addIfMissing = (dir, checkExists = true) => {
78
+ if (dir && (!checkExists || fs.existsSync(dir)) && !pathParts.includes(dir)) {
79
+ pathParts.unshift(dir);
80
+ }
81
+ };
82
+ // Node runtime directory
83
+ if (process.execPath) {
84
+ addIfMissing(path.dirname(process.execPath));
85
+ }
86
+ // Local user binaries (where Google's official android CLI lives)
87
+ addIfMissing(path.join(os.homedir(), '.local', 'bin'), false);
88
+ // Package managers and developer toolchains
89
+ addIfMissing('/opt/homebrew/bin');
90
+ addIfMissing('/opt/homebrew/sbin');
91
+ addIfMissing('/usr/local/bin');
92
+ addIfMissing(path.join(os.homedir(), '.cargo', 'bin'));
93
+ // Android SDK binaries
94
+ const androidHome = env.ANDROID_HOME;
95
+ if (androidHome) {
96
+ addIfMissing(path.join(androidHome, 'platform-tools'), false);
97
+ addIfMissing(path.join(androidHome, 'cmdline-tools', 'latest', 'bin'), false);
98
+ addIfMissing(path.join(androidHome, 'emulator'), false);
99
+ addIfMissing(path.join(androidHome, 'tools', 'bin'), false);
100
+ addIfMissing(path.join(androidHome, 'tools'), false);
101
+ const buildToolsDir = path.join(androidHome, 'build-tools');
102
+ if (fs.existsSync(buildToolsDir)) {
103
+ try {
104
+ const versions = fs.readdirSync(buildToolsDir).filter(v => !v.startsWith('.')).sort();
105
+ const latest = versions.at(-1);
106
+ if (latest) {
107
+ addIfMissing(path.join(buildToolsDir, latest));
108
+ }
109
+ }
110
+ catch { }
111
+ }
112
+ }
113
+ env.PATH = pathParts.join(path.delimiter);
114
+ return env;
115
+ }
43
116
  /**
44
117
  * Commands that are strictly read-only and idempotent.
45
118
  * Repeated identical executions within a short TTL window return cached output.
@@ -133,6 +206,7 @@ export const sysRun = {
133
206
  child = spawn(shell, shellArgs, {
134
207
  cwd,
135
208
  signal,
209
+ env: getEnhancedEnv(),
136
210
  ...(os.platform() === 'win32' ? { windowsVerbatimArguments: true } : {})
137
211
  });
138
212
  }
@@ -248,7 +322,7 @@ export const sysRun = {
248
322
  resolve(res);
249
323
  }
250
324
  else if (exitCode === 1 && !/[;&<>`$\r\n]/.test(rawCommand) && !rawCommand.includes('||')
251
- && /^(grep|egrep|fgrep|diff|test|which|cmp)\b/.test(rawCommand.split('|').at(-1)?.trim() || '')) {
325
+ && /(?:^|\s)(grep|egrep|fgrep|diff|test|which|cmp|pidof|pgrep)\b/.test(rawCommand.split('|').at(-1)?.trim() || '')) {
252
326
  // Semantic Exit Code Normalization: POSIX search tool found 0 matching lines.
253
327
  const res = {
254
328
  success: true,
@@ -268,11 +342,15 @@ export const sysRun = {
268
342
  resolve(res);
269
343
  }
270
344
  else {
345
+ let errorMsg = `Command exited with code ${exitCode}`;
346
+ if (cleanOutput.includes('more than one device/emulator')) {
347
+ errorMsg = 'adb failed: multiple devices/emulators connected. Pass `-s <serial>` (e.g. `adb -s emulator-5554 ...`) or set `export ANDROID_SERIAL=<serial>`.';
348
+ }
271
349
  if (signal && signal.aborted) {
272
350
  resolve({ success: false, output: cleanOutput, error: 'Process killed by user.', exitCode: 1 });
273
351
  }
274
352
  else {
275
- resolve({ success: false, output: cleanOutput, error: `Command exited with code ${exitCode}`, exitCode });
353
+ resolve({ success: false, output: cleanOutput, error: errorMsg, exitCode });
276
354
  }
277
355
  }
278
356
  });
@@ -304,6 +382,7 @@ export const sysRunBg = {
304
382
  cwd,
305
383
  detached: true,
306
384
  stdio: 'ignore',
385
+ env: getEnhancedEnv(),
307
386
  ...(os.platform() === 'win32' ? { windowsVerbatimArguments: true } : {})
308
387
  });
309
388
  child.once('error', error => resolve({ success: false, error: error.message }));
@@ -11,7 +11,9 @@
11
11
  */
12
12
 
13
13
  import { spawn } from 'node:child_process';
14
+ import fs from 'node:fs';
14
15
  import os from 'node:os';
16
+ import path from 'node:path';
15
17
  import { isLaunchOnlyPrompt, isServerLaunchCommand } from '../launchIntent.js';
16
18
 
17
19
  export interface SysRunArgs {
@@ -81,7 +83,84 @@ export function cleanSubprocessOutput(str: string): string {
81
83
  * Commands whose exit code 1 indicates a normal query result (e.g. no matches found,
82
84
  * differences found, or condition evaluated to false) rather than a system error.
83
85
  */
84
- export const SEARCH_COMMAND_PATTERN = /(^|\s|\||&&|;)(grep|egrep|fgrep|diff|test|which|cmp|find)(\s|$)/;
86
+ export const SEARCH_COMMAND_PATTERN = /(^|\s|\||&&|;)(grep|egrep|fgrep|diff|test|which|cmp|find|pidof|pgrep)(\s|$)/;
87
+
88
+ /**
89
+ * Resolves an enriched environment for subprocess execution with full toolchain
90
+ * awareness. Automatically discovers Android SDK, Homebrew, local user binaries,
91
+ * and language runtimes so ADB, emulator, and build tools function out-of-the-box.
92
+ */
93
+ export function getEnhancedEnv(baseEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
94
+ const env: NodeJS.ProcessEnv = { ...(baseEnv || process.env) };
95
+
96
+ // 1. Discover ANDROID_HOME / ANDROID_SDK_ROOT across standard platform locations
97
+ if (!env.ANDROID_HOME && !env.ANDROID_SDK_ROOT) {
98
+ const candidateDirs = [
99
+ path.join(os.homedir(), 'Library', 'Android', 'sdk'), // macOS
100
+ path.join(os.homedir(), 'Android', 'Sdk'), // Linux
101
+ '/usr/lib/android-sdk', // Linux package
102
+ path.join(process.env.LOCALAPPDATA || '', 'Android', 'Sdk') // Windows
103
+ ];
104
+ for (const dir of candidateDirs) {
105
+ if (dir && fs.existsSync(dir)) {
106
+ env.ANDROID_HOME = dir;
107
+ env.ANDROID_SDK_ROOT = dir;
108
+ break;
109
+ }
110
+ }
111
+ } else if (!env.ANDROID_HOME && env.ANDROID_SDK_ROOT) {
112
+ env.ANDROID_HOME = env.ANDROID_SDK_ROOT;
113
+ } else if (env.ANDROID_HOME && !env.ANDROID_SDK_ROOT) {
114
+ env.ANDROID_SDK_ROOT = env.ANDROID_HOME;
115
+ }
116
+
117
+ // 2. Build enriched PATH including dev tools, package managers, and Android toolchains
118
+ const currentPath = env.PATH || '';
119
+ const pathParts = currentPath.split(path.delimiter).filter(Boolean);
120
+ const addIfMissing = (dir: string, checkExists = true) => {
121
+ if (dir && (!checkExists || fs.existsSync(dir)) && !pathParts.includes(dir)) {
122
+ pathParts.unshift(dir);
123
+ }
124
+ };
125
+
126
+ // Node runtime directory
127
+ if (process.execPath) {
128
+ addIfMissing(path.dirname(process.execPath));
129
+ }
130
+
131
+ // Local user binaries (where Google's official android CLI lives)
132
+ addIfMissing(path.join(os.homedir(), '.local', 'bin'), false);
133
+
134
+ // Package managers and developer toolchains
135
+ addIfMissing('/opt/homebrew/bin');
136
+ addIfMissing('/opt/homebrew/sbin');
137
+ addIfMissing('/usr/local/bin');
138
+ addIfMissing(path.join(os.homedir(), '.cargo', 'bin'));
139
+
140
+ // Android SDK binaries
141
+ const androidHome = env.ANDROID_HOME;
142
+ if (androidHome) {
143
+ addIfMissing(path.join(androidHome, 'platform-tools'), false);
144
+ addIfMissing(path.join(androidHome, 'cmdline-tools', 'latest', 'bin'), false);
145
+ addIfMissing(path.join(androidHome, 'emulator'), false);
146
+ addIfMissing(path.join(androidHome, 'tools', 'bin'), false);
147
+ addIfMissing(path.join(androidHome, 'tools'), false);
148
+
149
+ const buildToolsDir = path.join(androidHome, 'build-tools');
150
+ if (fs.existsSync(buildToolsDir)) {
151
+ try {
152
+ const versions = fs.readdirSync(buildToolsDir).filter(v => !v.startsWith('.')).sort();
153
+ const latest = versions.at(-1);
154
+ if (latest) {
155
+ addIfMissing(path.join(buildToolsDir, latest));
156
+ }
157
+ } catch {}
158
+ }
159
+ }
160
+
161
+ env.PATH = pathParts.join(path.delimiter);
162
+ return env;
163
+ }
85
164
 
86
165
  /**
87
166
  * Commands that are strictly read-only and idempotent.
@@ -197,6 +276,7 @@ export const sysRun = {
197
276
  child = spawn(shell, shellArgs, {
198
277
  cwd,
199
278
  signal,
279
+ env: getEnhancedEnv(),
200
280
  ...(os.platform() === 'win32' ? { windowsVerbatimArguments: true } : {})
201
281
  });
202
282
  } catch (err: unknown) {
@@ -307,7 +387,7 @@ export const sysRun = {
307
387
  }
308
388
  resolve(res);
309
389
  } else if (exitCode === 1 && !/[;&<>`$\r\n]/.test(rawCommand) && !rawCommand.includes('||')
310
- && /^(grep|egrep|fgrep|diff|test|which|cmp)\b/.test(rawCommand.split('|').at(-1)?.trim() || '')) {
390
+ && /(?:^|\s)(grep|egrep|fgrep|diff|test|which|cmp|pidof|pgrep)\b/.test(rawCommand.split('|').at(-1)?.trim() || '')) {
311
391
  // Semantic Exit Code Normalization: POSIX search tool found 0 matching lines.
312
392
  const res: SysRunResult = {
313
393
  success: true,
@@ -326,10 +406,14 @@ export const sysRun = {
326
406
  }
327
407
  resolve(res);
328
408
  } else {
409
+ let errorMsg = `Command exited with code ${exitCode}`;
410
+ if (cleanOutput.includes('more than one device/emulator')) {
411
+ errorMsg = 'adb failed: multiple devices/emulators connected. Pass `-s <serial>` (e.g. `adb -s emulator-5554 ...`) or set `export ANDROID_SERIAL=<serial>`.';
412
+ }
329
413
  if (signal && signal.aborted) {
330
414
  resolve({ success: false, output: cleanOutput, error: 'Process killed by user.', exitCode: 1 });
331
415
  } else {
332
- resolve({ success: false, output: cleanOutput, error: `Command exited with code ${exitCode}`, exitCode });
416
+ resolve({ success: false, output: cleanOutput, error: errorMsg, exitCode });
333
417
  }
334
418
  }
335
419
  });
@@ -363,6 +447,7 @@ export const sysRunBg = {
363
447
  cwd,
364
448
  detached: true,
365
449
  stdio: 'ignore',
450
+ env: getEnhancedEnv(),
366
451
  ...(os.platform() === 'win32' ? { windowsVerbatimArguments: true } : {})
367
452
  });
368
453
  child.once('error', error => resolve({ success: false, error: error.message }));